如何从jstl的foreach循环中获取索引值

编程入门 行业动态 更新时间:2024-10-24 15:21:25
本文介绍了如何从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循环中获取索引值

本文发布于:2023-10-09 18:36:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476461.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   jstl   foreach

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!