首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JQuery 5星级系统

JQuery 5星级系统
EN

Stack Overflow用户
提问于 2015-08-19 16:15:19
回答 2查看 1.9K关注 0票数 1

我正在用html、php和jquery创建一个5星级评级系统,我不知道当用户点击评级时如何停止星级评级。在我的代码中,当用户单击4颗星星时,警报框显示4颗星星,但是当用户从星星上移动鼠标时,星星显示0。

这是我的代码,我不会在这里发布css

HTML:

代码语言:javascript
复制
<div class="rating">
    <div class="ratings_stars" data-rating="1"></div>
    <div class="ratings_stars" data-rating="2"></div>
    <div class="ratings_stars" data-rating="3"></div>
    <div class="ratings_stars" data-rating="4"></div>
    <div class="ratings_stars" data-rating="5"></div> 
</div>

JQUERY:

代码语言:javascript
复制
$('.ratings_stars').hover(
    // Handles the mouseover
    function() {
        $(this).prevAll().andSelf().addClass('ratings_over');
        $(this).nextAll().removeClass('ratings_vote'); 
    },
    // Handles the mouseout
    function() {
        $(this).prevAll().andSelf().removeClass('ratings_over');

    }
);



$('.ratings_stars').click(function() {
   $('.ratings_stars').removeClass('selected'); // Removes the selected class from all of them
   $(this).addClass('selected'); // Adds the selected class to just the one you clicked

   var rating = $(this).data('rating');
   alert(rating);
    // Get the rating from the selected star
   $('#rating').val(rating); // Set the value of the hidden rating form element
});
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-19 16:39:53

猜是因为你还没说你想要发生什么。这可能是,您希望选择的评级,并在它之前的明星,以突出显示。

所以,而不是这个

代码语言:javascript
复制
$(this).addClass('selected');

您使用此方法,类似于以前的情况。

代码语言:javascript
复制
$(this).prevAll().andSelf().addClass('selected');

但是,我也会删除悬停类,这样用户在单击时就很明显了

代码语言:javascript
复制
$(this).prevAll().andSelf().addClass('selected').removeClass('ratings_over'); 

演示

票数 1
EN

Stack Overflow用户

发布于 2016-05-09 16:40:33

代码语言:javascript
复制
<!--SAVE AS WHATEVAUWANNA.HTML AND TEST-->
<html> 
    <head>
        <title>Rating System jQuery Plug by Aldanis Vigo</title>
        <script src='https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js'></script>
        <style type='text/css' language='css'>
            .record{
                opacity: .50;
            }
            #value-display{
                position:  relative;
                top:  -5px;
                margin-left: 10px;
                color:  orange;
                font-weight:  bold;
            }
        </style>
    </head>
    <body>
        <span value='0' id='ratingbar'>
            <img class='record' number='1'/>
            <img class='record' number='2'/>
            <img class='record' number='3'/>
            <img class='record' number='4'/>
            <img class='record' number='5'/>
            <span id='value-display'>0 / 5</span>
        </span>         
    </body>
    <script>
        //Change these variables to your liking!!
        var iconsrc = 'https://upload.wikimedia.org/wikipedia/commons/a/ae/Record2.png';
        var iconwidth = '20px';
        var iconheight = '20px';

        var value = $('#ratingbar').attr('value');

        $('#ratingbar img').each(function(){
            //Set the icon for each
            $(this).attr('src', iconsrc);
            $(this).attr('width', iconwidth);
            $(this).attr('height', iconheight);

            $(this).hover( function(){
                $(this).css('opacity','1');
                $(this).prevAll().css('opacity','1');           
            });

            $(this).click( function(){
                //Clear all of them
                $(this).parent().attr('value',$(this).attr('number'));
                $(this).parent().children('#value-display').html($(this).attr('number') + ' / 5');                                

                                                 //Color up to the selected ones.
                $('#ratingbar img').each( function(){
                    if($(this).attr('number') <= $(this).parent().attr('value')){
                        $(this).css('opacity','1');
                    }else{
                        $(this).css('opacity','.50');
                    }
                }); 
            });

            $(this).mouseout( function(){
                $(this).css('opacity','.50');
                $(this).prevAll().css('opacity','.50');
                //Color up to the selected ones.
                $('#ratingbar img').each( function(){
                    if($(this).attr('number') <= $(this).parent().attr('value')){
                        $(this).css('opacity','1');
                    }
                });
            });
        });     
    </script>
</html>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32100770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档