是否可以在任何现有onsubmit / submit之前绑定submit()函数? [英] Is is possible to bind a submit() function before any existing onsubmit/submit?

查看:69
本文介绍了是否可以在任何现有onsubmit / submit之前绑定submit()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 onsubmit 属性的表单。我需要绑定一个新的提交事件,我需要在任何现有的提交函数之前执行这个事件。

I have a form with an onsubmit attribute. I need to bind a new submit event and I need this one to be executed before any existing submit functions.

以下代码演示了这个问题。

The following code demonstrates the problem.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Test</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
      jQuery(function($) {
        // a plugin
        $('form').submit(function() {
          alert("Second");
        });
        // an other plugin
        $('form').submit(function() {
          alert("Third");
        });

        // this event must always be executed as first event
        $('form').submit(function() {
          alert("Always First");
        });
      });
    </script>
  </head>
  <body>
    <form onsubmit="javascript:alert('Fourth');">
      <p>
        <input type="submit">
      </p>
    </form>
  </body>
</html>

如果执行脚本,首先得到Second,然后是First。

If you execute the script, first you get "Second", then "First".

是否可以绑定新的提交事件并指定是否必须在任何现有事件之前调用该函数?

Is is possible to bind a new submit event and specify whether the function must be called before any existing event?

约束:


  • 必须保留现有事件。

  • 我无法删除现有事件,因为内容 onsubmit 属性包含由Rails编写的非常复杂的逻辑

  • (ADDED):事件应始终在任何现有的onsubmit之前执行事件和已经绑定的事件(可能由其他插件绑定)

  • Existing events must be preserved.
  • I can't remove existing events because the content of the onsubmit attribute contains a quite complex logic written by Rails
  • (ADDED): the event should always be executed before any existing onsubmit event and already binded events (perhaps binded by an other plugin)

任何想法?

推荐答案

内联提交事件首先触发,您可以获取对它的引用,使 onsubmit 属性无效 form 元素,然后绑定你的新提交事件,这个将执行你的提交处理程序:

The inline submit event fires first, you could get a reference to it, nullify the onsubmit attribute on the form element, and then bind your new submit event, this one will execute your old submit handler:

  jQuery(function($) {
    var form = $('form'), oldSubmit = form[0].onsubmit;
    form[0].onsubmit = null;

    $('form').submit(function() {
      alert("First");
      oldSubmit.call(this); // preserve the context
    });
  });

请注意,我使用调用方法调用旧的提交处理程序,这是为了保留该函数中的 this 关键字,它将是表单元素本身。

Note that I use the call method to invoke your old submit handler, this is to preserve the this keyword inside that function, it will be the form element itself.

如果您的原始 onsubmit 处理程序有一些验证行为,您可以通过<$ c $获取其返回值c> var ret = oldSubmit.call(this);

If your original onsubmit handler has some validation behavior, you can get its return value by var ret = oldSubmit.call(this);

检查上面的例子这里

这篇关于是否可以在任何现有onsubmit / submit之前绑定submit()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆