1, jsp code
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <% List list = new ArrayList(); for (int i = 0; i <= 9; i++) { list.add(i); } session.setAttribute("list", list); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>jsp,el expression,for loop</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <script type="text/javascript" src='comm/js/jquery-1.8.3.min.js'></script> <script language="javascript"> function Mytest(i) { //Click the button to pop up the corresponding input box var test = $("#test"+i).val(); alert("click"+i+"No. button, the content you entered is[ "+test+" ]"); } </script> </head> <style type="text/css"> .foreach_tr1 { height: 6px; border: 2px solid #888; text-align: center; background-color: #F0F0F0; } .foreach_tr2 { height: 6px; border: 2px solid #888; text-align: center; background-color: #F0F0F0; } .span { margin-right: 40px; color: #FF2D2D; font-weight: bold; } .input1 { width: 180px; border-right: 60px; } .input2 { width: 60px; background-color: #A7DBFF; } </style> <body> <div class="class"> <table border="1" width="320px"> <tr align="center"> <td style="background-color: #Ccccff "> Chestnut: the foreach loop obtains the corresponding id value</td> </tr> <c:forEach items="${list}" varStatus="i" var="item"> <c:if test="${i.index % 2 == 0}"> <tr align="center" class="foreach_tr1"> </c:if> <c:if test="${i.index % 2 == 1}"> <tr align="center" class="foreach_tr2"> </c:if> <td><span class="span">${item}</span> <input type="text" id="test${i.index}" name="test${i.index}" class="input1" value="" /> <input type="button" class="input2" value="Submit" onclick="Mytest('${i.index}',)" /></td> </tr> </c:forEach> </table> </div> </body> </html>
2, Function description
Click the button number to pop up the corresponding input box according to the number of buttons cycled
3, Problem solving
1. problem description
The input from the foreach loop of the el expression can only get the val in the first text box?
2. cause analysis
This is because the input generated by the foreach loop is the same, with the same id and name. Of course, only the first one can be obtained when obtaining val through id and name. Then we need to make the input labels generated by the foreach loop different, such as adding a prefix or suffix, just to ensure uniqueness.
3. solutions
supplement
4, varStatus attribute description
1.c:forEach varStatus attribute
Current: the item (in the collection) of the current iteration
Index: the iteration index of the current iteration starting from 0
Count: the iteration count of the current iteration starting from 1
First: a flag indicating whether the current iteration is the first iteration
Last: a flag indicating whether the current iteration is the last iteration
begin: start sequence number
End: end sequence number
Step: jump step
2.varStatus attribute description
${status.index} output line number, starting from 0.
${status.count} output line number, starting from 1.
${status.current} items in the current iteration
${status.first} determines whether the current item is the first item in the collection, and the return value is true or false.
${status.last} determines whether the current item is the last item in the collection, and the return value is true or false.
begin, end and step respectively represent: start sequence number, end sequence number and jump step.
3. examples
For example: <c:foreach begin='6'end='15' step='3'items='${list}' var='item'>
Means: operate 6~15 pieces of data in the list set. Instead of cycling one by one, the values are taken every 3. That is, the 6th, 9th, 12th and 15th data in the operation set.