一、jsp代码 
 
<%@ 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表达式,for循环</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) {
		//点击几号按钮弹出对应输入框内容    	
    	var test = $("#test"+i).val();    
        alert("点击"+i+"号按钮,你输入的内容为【  "+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">栗子:foreach循环获取相应id值</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="提交"
					onclick="Mytest('${i.index}',)" /></td>
				</tr>
			</c:forEach>
		</table>
	</div>
</body>
</html> 
二、功能描述
 
根据循环出来的按钮数,点击按钮号码弹出相对应输入框内容
 
 
 
 
 
三、问题解答
 
1.问题描述
 
如何解决el表达式foreach循环出来的input 只能获取第一个文本框中的val?
 
2.原因分析
 
这是由于foreach循环生成的input都是一样的,相同的id,相同的name,通过id,name来获取val时当然只能获取到第一个了,那么我们就需要让foreach循环生成的input标签不一样,比如加上前缀或后缀,只要保证唯一性就可以了。
 
3.解决方案
 
 
补充
 
四、varStatus属性描述
 
1.c:forEach varStatus属性
 
current:当前这次迭代的(集合中的)项 index:当前这次迭代从 0 开始的迭代索引 count:当前这次迭代从 1 开始的迭代计数 first:用来表明当前这轮迭代是否为第一次迭代的标志 last:用来表明当前这轮迭代是否为最后一次迭代的标志 begin:起始序号 end:结束序号 step:跳跃步伐
 
2.varStatus属性说明
 
${status.current}:当前这次迭代的(集合中的)项。
 
${status.index}:输出行号,从0开始。
 
${status.count}:输出行号,从1开始。
 
${status.first}:判断当前项是否为集合中的第一项,返回值为true或false。
 
${status.last}:判断当前项是否为集合中的最后一项,返回值为true或false。
 
begin、end、step分别表示:起始序号,结束序号,跳跃步伐。
 
3.示例
 
如:<c:forEach begin='6' end='15' step='3' items='${list}' var='item'>
 
表示:操作list集合中6~15条数据,不是逐条循环,而是按照每3个取值。即操作集合中的第6、9、12、15条数据。