jQuery星级评分消息反映在所有项目上

问题描述 投票:0回答:1

我已经在jQuery中创建了一个星级,但是例如当我对第一个项目(例如2星)进行评级时,即使没有评级,反映此评级的消息也会显示在所有项目上。这是创建HTML的jQuery部分

'<section  class ="rating-widget"><div class="rating-stars text-center"><ul id="stars"><li class="star" title="Poor" data-value="1"><i class="fa fa-star fa-fw"></i></li><li class="star" title="Fair" data-value="2"><i class="fa fa-star fa-fw"></i></li><li class="star" title="Good" data-value="3"><i class="fa fa-star fa-fw"></i></li><li class="star" title="Excellent" data-value="4"><i class="fa fa-star fa-fw"></i></li><li class="star" title="Perfect" data-value="5"><i class="fa fa-star fa-fw"></i></li></ul></div>' +
                    '<div class="success-box"><div class="clearfix"></div><div class="text-message"></div><div class="clearfix"></div></div></section>';

这是创建评级功能的jQuery代码

  /* 1. Visualizing things on Hover - See next part for action on click */
            $('#stars li').on('mouseover', function() {
                var onStar = parseInt($(this).data('value'), 10); // The star currently mouse on

                // Now highlight all the stars that's not after the current hovered star
                $(this).parent().children('li.star').each(function(e) {
                    if (e < onStar) {
                        $(this).addClass('hover');
                    } else {
                        $(this).removeClass('hover');
                    }
                });

            }).on('mouseout', function() {
                $(this).parent().children('li.star').each(function(e) {
                    $(this).removeClass('hover');
                });
            });


            /* 2. Action to perform on click */
            $('#stars li').on('click', function() {
                var onStar = parseInt($(this).data('value'), 10); // The star currently selected
                var stars = $(this).parent().children('li.star');

                for (i = 0; i < stars.length; i++) {
                    $(stars[i]).removeClass('selected');
                }

                for (i = 0; i < onStar; i++) {
                    $(stars[i]).addClass('selected');
                }

                // JUST RESPONSE (Not needed)
                var ratingValue = parseInt($('#stars li.selected').last().data('value'), 10);
                var msg = "";
                if (ratingValue > 1) {
                    msg = "Thanks! You rated this " + ratingValue + " stars.";
                } else {
                    msg = "We will improve ourselves. You rated this " + ratingValue + " stars.";
                }
                responseMessage(msg);

            });

            function responseMessage(msg) {
                $('.success-box').fadeIn(200);
                $('.success-box div.text-message').html("<span>" + msg + "</span>");
            }

此图像显示消息在所有项目enter image description here上的显示方式

javascript jquery rating
1个回答
1
投票

您将需要更具体地使用jQuery。你的$('#stars li')很可能太笼统了。也许将项目星号分组在单独的divs中,以便可以更具体地引用它们。如果你的jQuery影响了太多的元素,那只意味着你的id和/或类引用过于笼统。

© www.soinside.com 2019 - 2024. All rights reserved.