删除jsp中点击按钮对应的行
userdetails.jsp
<tr>
<td>
<%
out.println(rs.getString(1));
name=rs.getString(1);
out.print("<input type='hidden' name='user' value='"+name+"'");
%>
</td>
<td>
<%out.println(rs.getString(2));
%>
</td>
<td>
<%out.println(rs.getString(3));
%>
</td>
<td>
<%out.println(rs.getString(4));
%>
</td>
<td>
<input type="Submit" name="delete_user" value="Delete"/>
</td>
</tr>
当我单击删除按钮时,只有第一行被删除,而不是与单击的按钮对应的行
userdetails.jsp
<tr>
<td>
<%
out.println(rs.getString(1));
name=rs.getString(1);
out.print("<input type='hidden' name='user' value='"+name+"'");
%>
</td>
<td>
<%out.println(rs.getString(2));
%>
</td>
<td>
<%out.println(rs.getString(3));
%>
</td>
<td>
<%out.println(rs.getString(4));
%>
</td>
<td>
<input type="Submit" name="delete_user" value="Delete"/>
</td>
</tr>
when i click the delete button only first row is getting deleted and not the row corresponding to which button is clicked
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将多个隐藏的输入值和删除按钮一起放入同一个表单中。当您使用 request.getParameter() 获取隐藏的输入值时,无论是否按下删除按钮,您实际上只会获取第一个值。
您需要将隐藏输入和删除按钮分组到它们自己的表单中。
这样,请求将始终将一个正确的用户名作为参数。
据说,在 JSP 中使用 scriptlet 是 90 年代编写 JSP 的方式,而且在 JSP 中混合数据库逻辑并不是一个好的做法。我建议您自己完成这个答案。
You're putting multiple hidden input values and delete buttons altogether in the same form. When you use
request.getParameter()
to get the hidden input value, you will indeed only get the first one, regardless of the delete button pressed.You need to group the hidden input and the delete button in their own forms.
This way the request will always have the one and the right user name as parameter.
Said that, using scriptlets in JSP is 90's way of writing JSPs and also mingling database logic in a JSP is not really a good practice. I'd suggest to get yourself through this answer.
您可以在单击按钮时将用户名添加到按钮值中:
然后在服务器端代码中解析该值:获取第一个空格后的文本,这是要删除的用户名并仅引用具有该用户名的行。
You can add the user name to the button value upon clicking it:
Then in the server side code, parse the value: get the text after first space, which is the user name to delete and reference only row with that user name.