php - 单击星标时无法正常工作

标签 php jquery html css

<分区>

我已经为评级系统创建了一个小代码,并且在我的表单中使用了 font awesome star。当我点击它时它不能正常工作。我想让评级系统在我点击它时正常工作。

这是我正在使用的代码:

jQuery(document).ready(function ($) {
  $('.ratings_stars').hover(
        // Handles the mouseover
        function() {
            $(this).prevAll().andSelf().addClass('ratings_over');
            $(this).next('i').slideToggle('500');
             $(this).find('i').toggleClass('fa-star fa-star-o');
        },
        // Handles the mouseout
        function() {
            $(this).prevAll().andSelf().removeClass('ratings_over');
            $(this).next('i').slideToggle('500');
             $(this).find('i').toggleClass('fa-star fa-star-o');
        }
    );
    
    function set_votes(widget) {
      jQuery('.star_' + widget).prevAll().andSelf().addClass('ratings_vote');
    }
    $('.ratings_stars').bind('click', function() {
        var star = this;
        $(star).prevAll().removeClass('ratings_over');
        $(star).prevAll().andSelf().addClass('ratings_over');
        $(this).next('i').slideToggle('500');
         $(this).find('i').toggleClass('fa-star fa-star-o');
 		$(star).nextAll().removeClass('ratings_vote');
        $('#client_ratings').val( jQuery(star).attr('data-id') );
          var data_id =jQuery(star).attr('data-id');
         jQuery('.star_' + data_id).prevAll().andSelf().addClass('ratings_vote');    
    }); 
    jQuery('.rate_widgett').each(function(i) {   
        $(this).nextAll().removeClass('ratings_vote'); 
        $(this).prevAll().removeClass('ratings_vote'); 
        var data_count = $(this).attr('data-id');
        $(this).find('.star_' + data_count).prevAll().andSelf().addClass('ratings_vote');
    });
   });
.ratings_choice {
    overflow:   visible;
    padding:    10px;
    position:   relative;
    width:      180px;
    height:     32px;
}
.ratings_stars {
    float:      left;
    padding:    2px;
}
.ratings_vote {
    color: green;
}
.ratings_choice {
    font: 10px verdana, sans-serif;
    margin: 0 auto 40px auto;
    width: 180px;
}
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="iva_ratings_choice">
	<div id="r1" class="ratings_choice">
	<a class="star_1 ratings_stars" data-id="1"><i class="fa fa-star-o fa-3x"></i></a>
	<a class="star_2 ratings_stars" data-id="2"><i class="fa fa-star-o fa-3x"></i></a>
	<a class="star_3 ratings_stars" data-id="3"><i class="fa fa-star-o fa-3x"></i></a>
	<a class="star_4 ratings_stars" data-id="4"><i class="fa fa-star-o fa-3x"></i></a>
	<a class="star_5 ratings_stars" data-id="5"><i class="fa fa-star-o fa-3x"></i></a>
</div></div>

<input type="hidden" name="client_ratings" id="client_ratings">

最佳答案

您只能使用 CSS 来完成。

纯 CSS 星级评分小工具

<fieldset class="rating">
<input type="radio" id="star5" name="rating" value="5" /><label class = "full" for="star5" title="Awesome - 5 stars"></label>
<input type="radio" id="star4half" name="rating" value="4 and a half" /><label class="half" for="star4half" title="Pretty good - 4.5 stars"></label>
<input type="radio" id="star4" name="rating" value="4" /><label class = "full" for="star4" title="Pretty good - 4 stars"></label>
<input type="radio" id="star3half" name="rating" value="3 and a half" /><label class="half" for="star3half" title="Meh - 3.5 stars"></label>
<input type="radio" id="star3" name="rating" value="3" /><label class = "full" for="star3" title="Meh - 3 stars"></label>
<input type="radio" id="star2half" name="rating" value="2 and a half" /><label class="half" for="star2half" title="Kinda bad - 2.5 stars"></label>
<input type="radio" id="star2" name="rating" value="2" /><label class = "full" for="star2" title="Kinda bad - 2 stars"></label>
<input type="radio" id="star1half" name="rating" value="1 and a half" /><label class="half" for="star1half" title="Meh - 1.5 stars"></label>
<input type="radio" id="star1" name="rating" value="1" /><label class = "full" for="star1" title="Sucks big time - 1 star"></label>
<input type="radio" id="starhalf" name="rating" value="half" /><label class="half" for="starhalf" title="Sucks big time - 0.5 stars"></label>
</fieldset>

和 CSS

@import url(//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css);
fieldset,
label {
    margin: 0;
    padding: 0;
}
body {
    margin: 20px;
}
h1 {
    font-size: 1.5em;
    margin: 10px;
}
/****** Style Star Rating Widget *****/

.rating {
    border: none;
    float: left;
}
.rating > input {
    display: none;
}
.rating > label:before {
    margin: 5px;
    font-size: 1.25em;
    font-family: FontAwesome;
    display: inline-block;
    content: "\f005";
}
.rating > .half:before {
    content: "\f089";
    position: absolute;
}
.rating > label {
    color: #ddd;
    float: right;
}
/***** CSS Magic to Highlight Stars on Hover *****/

.rating > input:checked ~ label,
/* show gold star when clicked */

.rating:not(:checked) > label:hover,
/* hover current star */

.rating:not(:checked) > label:hover ~ label {
    color: #FFD700;
}
/* hover previous stars in list */

.rating > input:checked + label:hover,
/* hover current star when changing rating */

.rating > input:checked ~ label:hover,
.rating > label:hover ~ input:checked ~ label,
/* lighten current selection */

.rating > input:checked ~ label:hover ~ label {
    color: #FFED85;
}

关注 demo
希望对你有帮助

谢谢,

关于php - 单击星标时无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40169106/

相关文章:

php - 插入命令的mysql错误

javascript - 如何将一个类添加到以前的 div

jquery - 使用 jQuery 有条件地设置属性

javascript - 如何使用输入类型范围读取属性目标

第二次调用时未定义 javascript 函数

php |动态API调用

javascript - 数据表格表单提交所有记录

javascript - 如何在点击事件中更改 html5 Canvas 的填充颜色?

html - 错误: Cannot find control with path: 'x ' angular 2

php - MYSQL & PHP 如何显示一个表中有多少个结果