日期:2014-05-20  浏览次数:20757 次

遍历取值问题
首先如下遍历可以取到很多id 
<c:forEach var = "rows" items="${getAllList}">
  <input type = "hidden" id = "funid" value = "${rows.funid }"/>
</c:forEach>

但我想用jquery取得每一个遍历的id值
  <script type="text/javascript">
$(function() {
var fid = "";
$("#funid").each(function(){
if(fid == "")
{
fid = $("#funid").val()+"/";
}else
{
fid += $("#funid").val()+"/";
}
});
alert(fid);
});
</script>

但是为什么总是只取到第一个id值,总觉得少了个触发事件,但是不懂得如何写,求高手指点!



------解决方案--------------------
第一种用varStatus--->va.index来区分ID
<c:forEach var = "rows" items="${getAllList}" varStatus="va">
<input type = "hidden" id = "funid" value = "${rows.funid }"/>
</c:forEach>


第二种方法(使用name来标示input控件):
<c:forEach var = "rows" items="${getAllList}" >
<input type = "hidden" name = "funid" value = "${rows.funid }"/>
</c:forEach>
在Script你可以写成:
$("input[name="funid]").each(function(){
}

------解决方案--------------------
Java code

$("#funid").each(function(){
if(fid == "")
{
fid = $("#funid").val()+"/";
}else
{
fid += $("#funid").val()+"/";
}
});
alert(fid);
});

用name属性取。。。id始终是一个值得呀。。。

------解决方案--------------------
HTML code

<c:forEach var = "rows" items="${getAllList}">
  <input type = "hidden" id = "funid" name="funid" value = "${rows.funid }"/>
</c:forEach>