如果我有30个动态按钮,那么我怎么知道哪个按钮是点击的,该按钮的值是什么:
<table align="center" cellspacing="3" cellpadding="3">
<%
for (int i = 1; i <= 10; i++) {
%>
<tr>
<%
for (int j = 1; j <= 3; j++) {
%>
<td><input type="button" name="b1"value="<%=k%>id=""></td>
<%
k++;
}
%>
</tr>
<%
}
%>
</table>
答案 0 :(得分:0)
首先你有错误
1 /
<td><input type="button" name="b1" value="<%=k%> id=""></td>
将其更改为
<td><input type="button" name="b1" value="<%=k%>" id=""></td>
2 / k你在哪里宣布它?你应该在循环之前声明它
1 /将您的按钮放在一个表单中,然后使用servlet获取值
2 /使用JAVASCRIPT
解决方案1的例子:
将您的jsp更改为
<form id="edit" action="MYSERVLET" method="post" >
<table align="center" cellspacing="3" cellpadding="3">
<%
int k=1;
for (int i = 1; i <= 10; i++) {
%>
<tr>
<%
for (int j = 1; j <= 3; j++) {
%>
<td><input type="submit" name="b1"value="<%=k%>" id=""></td>
<%
k++;
}
%>
</tr>
<%
}
%>
</table>
</form>
和servlet:
public class MYSERVLET extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String clickedButton= request.getParameter("b1");
request.setAttribute("val", clickedButton);
request.getRequestDispatcher("test.jsp").forward(request, response);
}
}
和下一个jsp(test.jsp)添加
<h1>VALUE OF BUTTON IS : ${val}</h1>
解决方案2的例子:
将您的输入更改为:
<td><input type="submit" name="b1"value="<%=k%>" id="" onclick='f1(this)'></td>
并在标记之前添加此脚本:
<script>
function f1(objButton){
alert(objButton.value);
}
</script>