简体   繁体   English

使用jQuery处理成功的表单提交

[英]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'); 成功提交表单后,我想在插件定义的成功处理程序之后执行其他操作,例如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. 我寻找了一个表单事件,该事件在onsubmit之后执行,可以将我的代码附加到该事件,但没有找到任何东西。

If you can't really change it, you could use .ajaxSuccess() to handle all the external ajax calls and filter the one you need: 如果您无法真正更改它,则可以使用.ajaxSuccess()处理所有外部ajax调用并过滤所需的调用:

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

Not pretty but it might work for you. 不漂亮,但可能适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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