简体   繁体   中英

handling successful form submission with jquery

In my application I'm using a plugin that generates the following markup:

<form id="addCommentForm" 
  action="/foo/add" 
  method="post" 
  onsubmit="
    jQuery.ajax({type:'POST', data:jQuery(this).serialize(), 
      url:'/foo/add',

      success:function(data,textStatus) {
        jQuery('#comments').html(data);
      },           
    });
    return false">

<!-- form elements here -->
</form>

When the form is submitted successfully I want to do something else after the success handler defined by the plugin, say alert('hello'); .

The reason I'm struggling with this is because I can't just add my code to the end of the success handler above, because this code is not under my control.

I looked for a form event that executes after onsubmit that I could attach my code to, but didn't find anything.

If you can't really change it, you could use .ajaxSuccess() to handle all the external ajax calls and filter the one you need:

$('form').ajaxSuccess(function(evt, request, settings) {
 if (settings.url == 'xxx')
     alert('test');
});

Not pretty but it might work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM