如何在jsp中获取动态按钮的值

如何在jsp中获取动态按钮的值,jsp,Jsp,如果我有30个动态按钮,那么我如何知道哪个按钮是单击按钮,以及该按钮的值是多少: <table align="center" cellspacing="3" cellpadding="3"> <% for (int i = 1; i <= 10; i++) { %> <tr> &l

如果我有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>


首先,你有错误

一,/

以及下一个jsp(test.jsp)添加

按钮的
值为:${val}
解决方案2的示例:

将您的输入更改为:

<td><input type="submit" name="b1"value="<%=k%>" id="" onclick='f1(this)'></td>

并在标记之前添加此脚本:

<script>
                
                function f1(objButton){  
                alert(objButton.value);
            }
                
                
            </script>

函数f1(objButton){
警报(objButton.value);
}

您可以为每个按钮添加id属性,也可以只使用唯一的名称。
<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>
    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); 
    
        }

}
<h1>VALUE OF BUTTON IS : ${val}</h1>
<td><input type="submit" name="b1"value="<%=k%>" id="" onclick='f1(this)'></td>
<script>
                
                function f1(objButton){  
                alert(objButton.value);
            }
                
                
            </script>