jstl:foreach循环设置值到隐藏变量总是返回零 [英] jstl : foreach loop setting values to a hidden variable returns zero always
本文介绍了jstl:foreach循环设置值到隐藏变量总是返回零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类似以下的foreach循环
I have a foreach loop like the following
<% if (request.getAttribute("categoriesList") != null) {%>
<c:forEach var="categoryName" items="${categoriesList}" varStatus="category">
<input type="hidden" id="categoryIndex" name="categoryIndex" value="${category.index}"/>
<li><a onclick="getCategoryIndex()" href="#">${categoryName}</a></li>
</c:forEach>
<% }%>
我尝试设置项目的索引值,如下所示
I try to set the index value of item like the following
< input type =hiddenid =categoryIndexname =categoryIndexvalue =$ {category.index}/>
当我这样做时
var categoryIndex = $('#categoryIndex').val();
console.log("categoryIndex : "+categoryIndex);
点击列出的项目,它一直打印 0
onClick of listed item it alsways prints 0
可能是什么问题?
推荐答案
你可以尝试一下像那样:
You could try it like that:
<c:forEach var="categoryName" items="${categoriesList}" varStatus="category">
<input type="hidden" id="categoryIndex+${category.index}" name="categoryIndex" value="${category.index}"/>
<li><a onclick="getCategoryIndex(${category.index})" href="#">${categoryName}</a></li>
</c:forEach>
在您的JavaScript方法中:
And in your JavaScript method:
function getCategoryIndex(x){
var categoryIndex = $('#categoryIndex'+x).val();
console.log("categoryIndex : "+categoryIndex);
}
这篇关于jstl:foreach循环设置值到隐藏变量总是返回零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文