【问题标题】:How to deal with 2 submit buttons in one form in struts2如何在struts2中以一种形式处理2个提交按钮
【发布时间】:2013-01-27 16:47:00
【问题描述】:

我正在使用 struts2 开发应用程序。我被困在一个地方,我有 2 个按钮,在一个单一的 struts2 形式中。我想要其中一个按钮,而不是调用默认的表单操作 bean,尽管基于此按钮 ID 调用它自己的 Jquery 模式对话框。每次我单击按钮时,它都会调用表单操作 bean。如何摆脱这种情况?

【问题讨论】:

    标签: java web-applications struts2


    【解决方案1】:

    在表单提交时不要直接调用操作。

    点击提交按钮时调用一个 JavaScript 方法,然后在方法内部控制行为。

    <script>
      /*
       function doSomething() {
        alert('Form submitted!');
        //perform your expected behavior
      } */
      function fun1(){
         //perform operation
      }
      function fun2(){
         //perform operation
      }
    </script>
    
    <form id="formid">
        <input type="submit" value="Submit1" onclick="fun1();">
        <input type="submit" value="Submit2" onclick="fun2();">
    </form>
    

    【讨论】:

      【解决方案2】:

      我使用按钮 ID 作为参考为 button2 创建了一个 Jquery 函数。单击该函数时会调用此函数。我已将以下代码行放在该函数的第一行。

      $('#button2id').click(function(event){
          event.preventDefault();
      });
      

      上面的代码行阻止调用默认的表单动作,即struts2动作bean。

      【讨论】:

        猜你喜欢
        • 2012-12-29
        • 2015-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-19
        • 2019-01-16
        • 2014-03-03
        相关资源
        最近更新 更多