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

时间:2021-06-24 22:34:17

I have a value set in the request object like the following,

我在请求对象中设置了一个值,如下所示,

String[] categoriesList=null;
categoriesList = engine.getCategoryNamesArray();
request.setAttribute("categoriesList", categoriesList );

and this is how I iterate in jsp page

这就是我在jsp页面中的迭代方式

<% if(request.getAttribute("categoriesList") != null) { %>
<c:forEach var="categoryName" items="${categoriesList}">
   <li><a onclick="getCategoryIndex()" href="#">${categoryName}</a></li>
</c:forEach>
<% }%>

How do I get index of each element and pass it to JavaScript function onclick="getCategoryIndex()".

如何获取每个元素的索引并将其传递给JavaScript函数onclick=“getCategoryIndex()”。

4 个解决方案

#1


175  

use varStatus to get the index c:forEach varStatus properties

使用varStatus获取索引c:forEach varStatus属性

<c:forEach var="categoryName" items="${categoriesList}" varStatus="loop">
    <li><a onclick="getCategoryIndex(${loop.index})" href="#">${categoryName}</a></li>
</c:forEach>

#2


11  

You can use the varStatus attribute like this:-

您可以像这样使用varStatus属性:-

<c:forEach var="categoryName" items="${categoriesList}" varStatus="myIndex">

myIndex.index will give you the index. Here myIndex is a LoopTagStatus object.

myIndex。index会给出index。这里的myIndex是一个LoopTagStatus对象。

Hence, you can send that to your javascript method like this:-

因此,您可以将它发送到您的javascript方法如下:-

<a onclick="getCategoryIndex(${myIndex.index})" href="#">${categoryName}</a>

#3


11  

I face Similar problem now I understand we have some more option : varStatus="loop", Here will be loop will variable which will hold the index of lop.

我现在遇到了类似的问题,我知道我们还有更多的选项:varStatus="loop",这里是loop will variable,它将保存lop的索引。

It can use for use to read for Zeor base index or 1 one base index.

它可以用于读取Zeor基索引或1个基索引。

${loop.count}` it will give 1 starting base index.

${loop.index} it will give 0 base index as normal Index of array start from 0.

$ {循环。索引}将以0作为数组的正常索引,将0作为基准索引。

For Example :

例如:

<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>

For more Info please refer this link

更多信息请参考这个链接

#4


0  

${categoryName}

$ { categoryName }

above line was giving error for me. so i wrote down in below way which is working fine for me. ')" href="#">${categoryName}

上面这一行给了我错误。所以我用下面的方法写下来这对我来说很好。')" href = " # " > $ { categoryName }

May be someone else might get same error. Look at this guys!!

也许其他人也会犯同样的错误。看看这个家伙! !

#1


175  

use varStatus to get the index c:forEach varStatus properties

使用varStatus获取索引c:forEach varStatus属性

<c:forEach var="categoryName" items="${categoriesList}" varStatus="loop">
    <li><a onclick="getCategoryIndex(${loop.index})" href="#">${categoryName}</a></li>
</c:forEach>

#2


11  

You can use the varStatus attribute like this:-

您可以像这样使用varStatus属性:-

<c:forEach var="categoryName" items="${categoriesList}" varStatus="myIndex">

myIndex.index will give you the index. Here myIndex is a LoopTagStatus object.

myIndex。index会给出index。这里的myIndex是一个LoopTagStatus对象。

Hence, you can send that to your javascript method like this:-

因此,您可以将它发送到您的javascript方法如下:-

<a onclick="getCategoryIndex(${myIndex.index})" href="#">${categoryName}</a>

#3


11  

I face Similar problem now I understand we have some more option : varStatus="loop", Here will be loop will variable which will hold the index of lop.

我现在遇到了类似的问题,我知道我们还有更多的选项:varStatus="loop",这里是loop will variable,它将保存lop的索引。

It can use for use to read for Zeor base index or 1 one base index.

它可以用于读取Zeor基索引或1个基索引。

${loop.count}` it will give 1 starting base index.

${loop.index} it will give 0 base index as normal Index of array start from 0.

$ {循环。索引}将以0作为数组的正常索引,将0作为基准索引。

For Example :

例如:

<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>

For more Info please refer this link

更多信息请参考这个链接

#4


0  

${categoryName}

$ { categoryName }

above line was giving error for me. so i wrote down in below way which is working fine for me. ')" href="#">${categoryName}

上面这一行给了我错误。所以我用下面的方法写下来这对我来说很好。')" href = " # " > $ { categoryName }

May be someone else might get same error. Look at this guys!!

也许其他人也会犯同样的错误。看看这个家伙! !