首先如下遍历可以取到很多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值,总觉得少了个触发事件,但是不懂得如何写,求高手指点!
<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值,总觉得少了个触发事件,但是不懂得如何写,求高手指点!
根据给定的ID匹配一个元素。
返回值 Element 不是Array<Element>
无论循环多少次,你的id都没有变。所有的hidden id 都是funid
<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(){
}
$("#funid").each(function(){
if(fid == "")
{
fid = $("#funid").val()+"/";
}else
{
fid += $("#funid").val()+"/";
}
});
alert(fid);
});用name属性取id始终是一个值得呀
<c:forEach var = "rows" items="${getAllList}">
<input type = "hidden" id = "funid" name="funid" value = "${rows.funid }"/>
</c:forEach>
$("input[name=funid]").each(function(){
alert($(this).val());
})