【问题标题】:Distinguish between two buttons in the same form method区分同一个表单中的两个按钮方法
【发布时间】:2012-09-29 22:20:23
【问题描述】:

我在同一个表单方法中有两个提交按钮(它们需要相同的信息),但是每个按钮必须对信息做不同的事情,所以我的问题是,当我在响应 servlet 中时,我怎么知道哪个来自请求 servlet 的按钮触发了表单? (我正在研究java)

【问题讨论】:

    标签: java servlets


    【解决方案1】:

    我认为您可以有一个额外的隐藏值来指示在 html 表单中单击了哪个按钮。这是一个例子

    <html>
    <head>
        <title>What</title>
    <script>
    function fun1(num)
    {
        if (num == 1)
        {
            document.getElementById('mybutton').value = "1";
        }
        else
        {
            document.getElementById('mybutton').value = "2";
        }
    }
    </script>
    </head>
    <body>
    <form name="form1" method="get" action="">
    <input type="hidden" id="mybutton" name="mybutton" value="x"/>
    <input type="submit" value="send" onclick="fun1(1)"/>
    <input type="submit" value="send" onclick="fun1(2)"/>
    </form>
    </body>
    </html>
    

    现在,当您从 servlet (req.getParameter()) 方法检查 mybutton 的值时。这将指示单击了哪个按钮。

    谢谢...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      • 2010-10-07
      相关资源
      最近更新 更多