本文介绍了如何从jstl的foreach循环中获取索引值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 request 对象中设置了一个值,如下所示,
I have a value set in the request object like the following,
String[] categoriesList=null; categoriesList = engine.getCategoryNamesArray(); request.setAttribute("categoriesList", categoriesList );这就是我在jsp页面中迭代的方式
and this is how I iterate in jsp page
<% if(request.getAttribute("categoriesList") != null) { %> <c:forEach var="categoryName" items="${categoriesList}"> <li><a onclick="getCategoryIndex()" href="#">${categoryName}</a></li> </c:forEach> <% }%>如何获取每个元素的索引并将其传递给 JavaScript 函数 onclick="getCategoryIndex()".
How do I get index of each element and pass it to JavaScript function onclick="getCategoryIndex()".
推荐答案use varStatus 获取索引 c:forEach varStatus 属性
use varStatus to get the index c:forEach varStatus properties
<c:forEach var="categoryName" items="${categoriesList}" varStatus="loop"> <li><a onclick="getCategoryIndex(${loop.index})" href="#">${categoryName}</a></li> </c:forEach>更多推荐
如何从jstl的foreach循环中获取索引值
发布评论