我有星级评分系统。
HTML部件通过php添加到foreach
中,并附带图片库以添加评级系统。我把这个代码作为例子在我通过jQuery设置检查的实际代码中使用DB_result工作得很好但是只有20个图像中的一个得到了显示的星星
我该如何解决?总共有5000张图片通过分页添加
PhP部分是这样的:
foreach ($xxx AS $X){
//cake php simply echo the file where the inputfields are in
echo $this->Element('../Candidates/Elements/rating_display')
}
// for the number 2 comes the php variable
$(".vote[value='2']").attr("checked", true);

.rating {
float:left;
}
/* :not(:checked) is a filter, so that browsers that don’t support :checked don’t
follow these rules. Every browser that supports :checked also supports :not(), so
it doesn’t make the test unnecessarily selective */
.rating:not(:checked) > input {
position:absolute;
top:-9999px;
clip:rect(0,0,0,0);
}
.rating:not(:checked) > label {
float:right;
width:1em;
padding:0 .1em;
overflow:hidden;
white-space:nowrap;
cursor:pointer;
font-size:200%;
line-height:1.2;
color:#ddd;
text-shadow:1px 1px #bbb, 2px 2px #666, .1em .1em .2em rgba(0,0,0,.5);
}
.rating:not(:checked) > label:before {
content: '★ ';
}
.rating > input:checked ~ label {
color: #f70;
text-shadow:1px 1px #c60, 2px 2px #940, .1em .1em .2em rgba(0,0,0,.5);
}
.rating:not(:checked) > label:hover,
.rating:not(:checked) > label:hover ~ label {
color: gold;
text-shadow:1px 1px goldenrod, 2px 2px #B57340, .1em .1em .2em rgba(0,0,0,.5);
}
.rating > input:checked + label:hover,
.rating > input:checked + label:hover ~ label,
.rating > input:checked ~ label:hover,
.rating > input:checked ~ label:hover ~ label,
.rating > label:hover ~ input:checked ~ label {
color: #ea0;
text-shadow:1px 1px goldenrod, 2px 2px #B57340, .1em .1em .2em rgba(0,0,0,.5);
}
.rating > label:active {
position:relative;
top:2px;
left:2px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<fieldset class="rating">
<legend>Please rate:</legend>
<input type="radio" id="star5" name="rating" value="5" class="vote"/><label for="star5" title="Rocks!">5 stars</label>
<input type="radio" id="star4" name="rating" value="4" class="vote" /><label for="star4" title="Pretty good">4 stars</label>
<input type="radio" id="star3" name="rating" value="3" class="vote"/><label for="star3" title="Meh">3 stars</label>
<input type="radio" id="star2" name="rating" value="2" class="vote"/><label for="star2" title="Kinda bad">2 stars</label>
<input type="radio" id="star1" name="rating" value="1" class="vote"/><label for="star1" title="Sucks big time">1 star</label>
</fieldset>
<fieldset class="rating">
<legend>Please rate:</legend>
<input type="radio" id="star5" name="rating" value="5" class="vote"/><label for="star5" title="Rocks!">5 stars</label>
<input type="radio" id="star4" name="rating" value="4" class="vote" /><label for="star4" title="Pretty good">4 stars</label>
<input type="radio" id="star3" name="rating" value="3" class="vote"/><label for="star3" title="Meh">3 stars</label>
<input type="radio" id="star2" name="rating" value="2" class="vote"/><label for="star2" title="Kinda bad">2 stars</label>
<input type="radio" id="star1" name="rating" value="1" class="vote"/><label for="star1" title="Sucks big time">1 star</label>
</fieldset>
&#13;
答案 0 :(得分:0)
它仅适用于一个实例,因为输入上有重复的ID。让它们与众不同 - 总是:)
答案 1 :(得分:0)
但它看起来好像你可以这样做:
$i=1;
foreach ($xxx AS $X){
//cake php simply echo the file where the inputfields are in
echo $this->Element('../Candidates/Elements/rating_display', array(
"i" => $i
));
$i++;
}
(我看过这里:http://book.cakephp.org/2.0/en/views.html)
在包含输入文件的文件中,您可以添加$ i,以使每个块都是唯一的。像这样:
<fieldset class="rating">
<legend>Please rate:</legend>
<input type="radio" id="<?=$i?>star5" name="rating" value="5" class="vote"/><label for="<?=$i?>star5" title="Rocks!">5 stars</label>
<input type="radio" id="<?=$i?>star4" name="rating" value="4" class="vote"/><label for="<?=$i?>star4" title="Pretty good">4 stars</label>
<input type="radio" id="<?=$i?>star3" name="rating" value="3" class="vote"/><label for="<?=$i?>star3" title="Meh">3 stars</label>
<input type="radio" id="<?=$i?>star2" name="rating" value="2" class="vote"/><label for="<?=$i?>star2" title="Kinda bad">2 stars</label>
<input type="radio" id="<?=$i?>star1" name="rating" value="1" class="vote"/><label for="<?=$i?>star1" title="Sucks big time">1 star</label>
我希望这会对你有所帮助。