最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is is possible to bind a submit() function before any existing onsubmitsubmit? - Stack Overflow

programmeradmin2浏览0评论

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=".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>

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?

Constraints:

  • 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)

Any idea?

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>

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?

Constraints:

  • 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)

Any idea?

Share Improve this question edited Jan 12, 2010 at 21:49 Simone Carletti asked Jan 12, 2010 at 20:28 Simone CarlettiSimone Carletti 176k50 gold badges367 silver badges369 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 15

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
    });
  });

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.

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

Check the above example here.

Take the existing submit handler and store it in a variable.

Then in your new handler, call that function that you stored from the previous submit handler, and re-assign your new one to the listener.

Example:

  jQuery(function($) {

    $('form').submit(function() {
         var first = this.onsubmit;
         //...
         first.call( this );
    });
  });

...or something like that. :=)

I think this is a better way if it works which it should because the OP has accepted it.

发布评论

评论列表(0)

  1. 暂无评论