做WEB项目时遇到一个问题,请大侠帮忙解决一下
本帖最后由 u011613626 于 2013-08-31 22:26:13 编辑 代码如下:
<%
TeacherService service=new TeacherService();
int TeachId=(Integer)session.getAttribute("TeachId");
List<SelTopic> data=service.getAllSelTopic(TeachId);
%>
<form name="MyForm" action="Teacher/Topic/SelTopic.jsp" method="post">
<table border="1" cellpadding="5" cellspacing="0" align="center" width="660">
<tr height="50">
<td width="120">学生编号</td>
<td width="120">学生姓名</td>
<td width="150">学生专业</td>
<td width="120">状态</td>
<td width="150" align="center">操作</td>
</tr>
<%for(SelTopic seltopic:data){ %>
<tr height="30">
<td ><%=seltopic.getStuNo() %></td>
<td ><%=seltopic.getStuName() %></td>
<td ><%=seltopic.getMajorName() %></td>
<td><% if(seltopic.getSelState()==0){ out.print("未通过"); } else{ out.print("通过"); } %></td>
<td align="center">
<input type="hidden" name="Id" value="<%= seltopic.getId() %>"/>
<input type="submit" name="Add" value="通过"/>
<input type="submit" name="Delete" value="拒绝"/>
</td>
</tr>
<%} %>
</table>
</form>
String Id=request.getParameter("Id");
不知道为什么点击任何一个按钮时,Id获得的总是列表里的第一个值,其对应的Id的值无法获取到,提交页面和处理页面是同一个页面。请各位大侠帮忙分析一下!
------解决方案--------------------
你是不是要做一个点击一个按钮,就拿到它对应的前面的隐藏域的值的功能?这个就稍微麻烦一点,我的做法是
定义一个《% int j=1; %》 然后<input type="hidden" name="Id<%=j-1 %>" value="<%= seltopic.getId() %>"/> <input type="submit" name="submit<%=j-1 %>" value="<%= seltopic.getId() %>"/>
这样每次的id都不同了 你先用js取得对应的隐藏域的值value 然后再在点击submit的时候调用一个方法把
取得的值用window.location.href =“xxxxxxxxx?idtmp=value” 的方式传到后台 这样你就可以用requedst.getparamter解析出来了
很麻烦 但肯定能用 我想不出更好的办法了
[解决办法]
感觉你的代码逻辑有问题,本意应该是提交table的一行记录,但是这样写的话会提交整个table,所以得到的id自然也是个数组,建议把每行设置为一个post提交
<table border="1" cellpadding="5" cellspacing="0" align="center" width="660">
<tr height="50">
<td width="120">学生编号</td>
<td width="120">学生姓名</td>
<td width="150">学生专业</td>
<td width="120">状态</td>
<td width="150" align="center">操作</td>
</tr>
<%for(SelTopic seltopic:data){ %>
<form name="MyForm" action="Teacher/Topic/SelTopic.jsp" method="post">
<tr height="30">
<td ><%=seltopic.getStuNo() %></td>
<td ><%=seltopic.getStuName() %></td>
<td ><%=seltopic.getMajorName() %></td>
<td><% if(seltopic.getSelState()==0){ out.print("未通过"); } else{ out.print("通过"); } %></td>
<td align="center">
<input type="hidden" name="Id" value="<%= seltopic.getId() %>"/>
<input type="submit" name="Add" value="通过"/>
<input type="submit" name="Delete" value="拒绝"/>
</td>
</tr>
</form>
<%} %>
</table>
这样应该能拿到你提交的id