我使用了此https://codepen.io/alisuarez/pen/RWGNLm
中使用的jQuery这里的问题是因为在函数中使用了类,所有星级评级同时工作。在jQuery中,当我使用ID时,它可以按预期用于一个特定的评级行。我正在动态地将行克隆成多行。如何为每个id star-rating1,star-rating2等运行jQuery? 任何帮助将不胜感激。提前谢谢!
var $star_rating = $('#star-rating .fa');
var SetRatingStar = function() {
return $star_rating.each(function() {
if (parseInt($star_rating.siblings('input.rating-value').val()) >=
parseInt($(this).data('rating'))) {
return $(this).removeClass('fa-star-o').addClass('fa-star');
} else {
return $(this).removeClass('fa-star').addClass('fa-star-o');
}
});
};
$star_rating.on('click', function() {
$star_rating.siblings('input.rating-value').val($(this).data('rating'));
return SetRatingStar();
});
SetRatingStar();
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="star-rating" id="star-rating">
<span class="fa fa-star-o" data-rating="1"></span>
<span class="fa fa-star-o" data-rating="2"></span>
<span class="fa fa-star-o" data-rating="3"></span>
<span class="fa fa-star-o" data-rating="4"></span>
<span class="fa fa-star-o" data-rating="5"></span>
<input type="hidden" name="whatever1" class="rating-value" value="2.56">
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<div class="star-rating" id="star-rating">
<span class="fa fa-star-o" data-rating="1"></span>
<span class="fa fa-star-o" data-rating="2"></span>
<span class="fa fa-star-o" data-rating="3"></span>
<span class="fa fa-star-o" data-rating="4"></span>
<span class="fa fa-star-o" data-rating="5"></span>
<input type="hidden" name="whatever2" class="rating-value" value="1.9">
</div>
</div>
</div>
答案 0 :(得分:0)
以下代码按预期工作
HTML
<div class="container">
<div class="row">
<div class="col-lg-12 abc">
<div class="star-rating">
<span class="fa fa-star-o" data-rating="1"></span>
<span class="fa fa-star-o" data-rating="2"></span>
<span class="fa fa-star-o" data-rating="3"></span>
<span class="fa fa-star-o" data-rating="4"></span>
<span class="fa fa-star-o" data-rating="5"></span>
<input type="hidden" name="whatever1" class="rating-value" value="2.56">
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12 abc">
<div class="star-rating">
<span class="fa fa-star-o" data-rating="1"></span>
<span class="fa fa-star-o" data-rating="2"></span>
<span class="fa fa-star-o" data-rating="3"></span>
<span class="fa fa-star-o" data-rating="4"></span>
<span class="fa fa-star-o" data-rating="5"></span>
<input type="hidden" name="whatever2" class="rating-value" value="1.9">
</div>
</div>
</div>
</div>
JQUERY
$('.abc').click(function(){
var $star_rating = $(this).find('.star-rating .fa');
var SetRatingStar = function() {
return $star_rating.each(function() {
if (parseInt($star_rating.siblings('input.rating-value').val()) >= parseInt($(this).data('rating'))) {
return $(this).removeClass('fa-star-o').addClass('fa-star');
} else {
return $(this).removeClass('fa-star').addClass('fa-star-o');
}
});
};
$star_rating.on('click', function() {
$star_rating.siblings('input.rating-value').val($(this).data('rating'));
return SetRatingStar();
});
});
SetRatingStar();
答案 1 :(得分:-1)
我建议将事件监听器绑定到星号父元素和事件委托:$star_rating.on('click', '.fa', function(){});
然后仅在更改评级的实例上运行星形设置功能。</ p>
// Set the variable to the star container.
var $star_rating = $('.star-rating');
// This runs the setStars function below for each and every rating instance.
var SetRatingStar = function() {
$star_rating.each(setStars);
};
function setStars() {
// We use $(this) to only run the code for the calling instance of
// $('.star-rating') rather than all
// Use a var to store the current rating value.
var ratingVal = parseInt( $(this).find('input.rating-value').val());
// Clear all filled classes and add open class to all stars.
$(this).children().removeClass('fa-star').addClass('fa-star-o');
// While the i var is less than the total rating value, use eq(i) to add
// the filled class to the star
for(var i = 0; i < ratingVal; i ++)
$(this).children().eq(i).removeClass('fa-star-o').addClass('fa-star');
}
$star_rating.on('click', '.fa', function() {
$(this).siblings('input.rating-value').val($(this).index() + 1);
// Calls setStars function with $(this) value set to the parent container.
setStars.call($(this).parent());
});
$(document).ready(function() {
SetRatingStar();
});
codepen上的工作示例: CODEPEN EXAMPLE