Jav*_*ons 98 java foreach jsp for-loop jstl
我在request
对象中设置了一个值,如下所示,
String[] categoriesList=null;
categoriesList = engine.getCategoryNamesArray();
request.setAttribute("categoriesList", categoriesList );
Run Code Online (Sandbox Code Playgroud)
这就是我在jsp页面中迭代的方式
<% if(request.getAttribute("categoriesList") != null) { %>
<c:forEach var="categoryName" items="${categoriesList}">
<li><a onclick="getCategoryIndex()" href="#">${categoryName}</a></li>
</c:forEach>
<% }%>
Run Code Online (Sandbox Code Playgroud)
如何获取每个元素的索引并将其传递给JavaScript函数onclick="getCategoryIndex()"
.
new*_*ser 219
使用varStatus获取索引 c:forEach varStatus属性
<c:forEach var="categoryName" items="${categoriesList}" varStatus="loop">
<li><a onclick="getCategoryIndex(${loop.index})" href="#">${categoryName}</a></li>
</c:forEach>
Run Code Online (Sandbox Code Playgroud)
Lax*_*n G 16
我面临类似问题现在我明白我们还有一些选择:varStatus ="loop",这里将是loop将变量保存为lop的索引.
它可以用于读取Zeor基本索引或1个基本索引.
${loop.count}` it will give 1 starting base index.
Run Code Online (Sandbox Code Playgroud)
${loop.index} it will give 0 base index as normal Index of array
从0开始.
例如 :
<c:forEach var="currentImage" items="${cityBannerImages}" varStatus="loop">
<picture>
<source srcset="${currentImage}" media="(min-width: 1000px)"></source>
<source srcset="${cityMobileImages[loop.count]}" media="(min-width:600px)"></source>
<img srcset="${cityMobileImages[loop.count]}" alt=""></img>
</picture>
</c:forEach>
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅此链接
Sud*_*hul 11
你可以使用这样的varStatus
属性: -
<c:forEach var="categoryName" items="${categoriesList}" varStatus="myIndex">
Run Code Online (Sandbox Code Playgroud)
myIndex.index将为您提供索引.这myIndex
是一个LoopTagStatus对象.
因此,您可以将其发送到您的javascript方法,如下所示: -
<a onclick="getCategoryIndex(${myIndex.index})" href="#">${categoryName}</a>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
219519 次 |
最近记录: |