【发布时间】:2015-03-09 00:17:27
【问题描述】:
我正在尝试使用 jquery 循环遍历每个 id = "rate" 的 div 标签。 jquery 然后将在单独的 div 上执行功能。这是我的代码:
info.html
<html>
<input class="rate" type="hidden" value="<?php echo $rate;?>" /></p>
<div class='rating_bar'>
<!-- div element that contains full stars with percentage width,
which represents rating -->
<div class="rating" style='width:40%;'>
</div>
</div>
</html>
rate.js
$(document).ready(function() {
$('.rate').each(function( index ){
var value = this.value;
if(value >= 0 && value <= 5)
{
value = Math.round((value/5)*100);
$('.rating').each(function(){
$(this).width(value + '%');
}
}}
else
{
alert("Incorrect value, rating must be between 0 and 5");
}
});
});
style.css
.rating_bar {
width: 150px;
height: 40px;
background: url(../images/rate-btn2.png);
background-repeat: repeat-x;
background-position: 0 0;
text-align: left;
}
.rating {
height: 40px;
background: url(../images/rate-btn2-hover.png);
background-position: 0 -1px;
background-repeat: repeat-x;
}
问题:
我有多个 div id="rate" 的隐藏文本框。 jquery 只影响第一个 div id。如何循环遍历每个 div id?
【问题讨论】:
-
你不能有多个具有相同
ID的div -
使用类而不是 ID。这就是类的用途......多个实例
-
@DhirajBodicherla ,我已将 id 替换为 class,仍然无法正常工作:(
-
你把选择器改成类选择器了吗?
-
在
each部分中,您可以将document.getElementById("rate").value;替换为$(this).val()或this.value以获取您正在迭代的输入的值。
标签: javascript php jquery html css