代码之家  ›  专栏  ›  技术社区  ›  aarona

我能确定用$(form)点击了哪个按钮。提交吗?

  •  1
  • aarona  · 技术社区  · 15 年前

    <!DOCTYPE html5>
    <html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
      $(document).ready(function() {
        $('#form').submit(function(e) {
          // put your breakpoint here to look at e
          alert('Which button was clicked?');
        });
      });
    </script>
    </head>
    <body>
      <form id="form">
        <input name="text" type="text"><br/>
        <input name="commit" type="submit" value="Save"/>
        <input name="commit" type="submit" value="Cancel"/>
      </form>
    </body>
    

    我需要知道submit handler方法单击了哪个按钮。使用 $.click 经办人 不会的 $.submit

    2 回复  |  直到 15 年前
        1
  •  2
  •   jwueller    15 年前

    请尝试以下操作:

    $('#form').submit(function(e) {
        console.log(e.originalEvent.explicitOriginalTarget);
    });
    

    这只不过是壁虎,所以你应该寻找其他的解决方案。不过,我一个也不知道。

        2
  •  0
  •   aarona    14 年前

    现在,我所知道的所有浏览器都无法做到这一点。

    推荐文章