我正在处理一个星级评分页面,我正在使用rateYo插件(http://rateyo.fundoocode.ninja)。如果我使用一个星级评级,一切正常,但我需要在此页面上动态使用它,从数据库中获取产品数据。我最初的想法是使用类与ID在页面上有多个并使用带有一些模拟数据的循环:
var demoRatings = [3.5, 4, 2, 1.5, 5, 4.5, 2.5, 1],
stars = $('.rateYo');
for (var i = 0; i < stars.length; i++) {
$('.rateYo').rateYo({
halfStar: true,
rating: demoRatings[i],
readOnly: true
});
};
虽然我没有尝试这个错误并显示所有评级部分,但它只显示数组中第一个项目的星星(3.5)。知道我在这里缺少什么,或者这可以像这样使用吗?我将包含小提琴链接并使用下面的代码片段。谢谢你的帮助。
jsfiddle:https://jsfiddle.net/m6fyem7e/5/
var demoRatings = [3.5, 4, 2],
stars = $('.rateYo');
for (var i = 0; i < stars.length; i++) {
$('.rateYo').rateYo({
halfStar: true,
rating: demoRatings[i],
readOnly: true
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/rateYo/2.3.2/jquery.rateyo.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rateYo/2.3.2/jquery.rateyo.min.js"></script>
<div class="item">
<p class="product-name">product name</p>
<div class="star-rating rateYo"></div>
<p class="price">$100.00</p>
<p class="starting-at">Starting at: <span>$50.00</span></p>
<div class="add-to-cart">
<button><i class="fa fa-shopping-cart" aria-hidden="true"></i>Add to Cart</button>
</div>
</div>
<div class="item">
<p class="product-name">product name</p>
<div class="star-rating rateYo"></div>
<p class="price">$100.00</p>
<p class="starting-at">Starting at: <span>$50.00</span></p>
<div class="add-to-cart">
<button><i class="fa fa-shopping-cart" aria-hidden="true"></i>Add to Cart</button>
</div>
</div>
<div class="item">
<p class="product-name">product name</p>
<div class="star-rating rateYo"></div>
<p class="price">$100.00</p>
<p class="starting-at">Starting at: <span>$50.00</span></p>
<div class="add-to-cart">
<button><i class="fa fa-shopping-cart" aria-hidden="true"></i>Add to Cart</button>
</div>
</div>
答案 0 :(得分:1)
问题在于您尝试更改已初始化的元素上的RateYo设置。例如,您将代码更改为
query.setHint("toplink.jdbc.fetch-size", "100");
它会起作用。
以下是使用元素中的 data - 属性的示例。
for (var i = 0; i < stars.length; i++) {
$('.rateYo').eq(i).rateYo({ // select by index as an example
halfStar: true,
rating: demoRatings[i],
readOnly: true
});
}
$('.rateYo').each(function() {
$(this).rateYo({
halfStar: true,
rating: this.dataset.rating,
readOnly: true
});
});
答案 1 :(得分:1)
使用each from jQuery的绝佳机会:
var demoRatings = [3.5, 4, 2],
stars = $('.rateYo');
stars.each(function(i) {
$(this).rateYo({
halfStar: true,
rating: demoRatings[i],
readOnly: true
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/rateYo/2.3.2/jquery.rateyo.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rateYo/2.3.2/jquery.rateyo.min.js"></script>
<div class="item">
<p class="product-name">product name</p>
<div class="star-rating rateYo"></div>
<p class="price">$100.00</p>
<p class="starting-at">Starting at: <span>$50.00</span></p>
<div class="add-to-cart">
<button><i class="fa fa-shopping-cart" aria-hidden="true"></i>Add to Cart</button>
</div>
</div>
<div class="item">
<p class="product-name">product name</p>
<div class="star-rating rateYo"></div>
<p class="price">$100.00</p>
<p class="starting-at">Starting at: <span>$50.00</span></p>
<div class="add-to-cart">
<button><i class="fa fa-shopping-cart" aria-hidden="true"></i>Add to Cart</button>
</div>
</div>
<div class="item">
<p class="product-name">product name</p>
<div class="star-rating rateYo"></div>
<p class="price">$100.00</p>
<p class="starting-at">Starting at: <span>$50.00</span></p>
<div class="add-to-cart">
<button><i class="fa fa-shopping-cart" aria-hidden="true"></i>Add to Cart</button>
</div>
</div>