如何在ajax调用期间停止表单提交 [英] How to stop form submit during ajax call

查看:99
本文介绍了如何在ajax调用期间停止表单提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只能修改ajax调用中的代码. 当我单击名为$('#form1')的表单中的提交时,就会发生ajax调用.

I can only modify the code in the ajax call. The ajax call occurs when I click the submit in form named $('#form1').

$('#form1').submit(function () {
    $.ajax({
       url: 'some.php',
       type: 'POST',
       data: somedata,
       success: function (msg) {
          if (!msg) {
             // I wanna to stop '#form1' submit here,how to do that? I can only modify the code in the ajax call.
          }
       }
    });
 });

推荐答案

您需要在成功处理程序之前将其停止.因为该函数在您的AJAX调用后完成执行,所以表单将在进行ajax调用时提交(并且在您的ajax调用结束时为时已晚).

You'll need to stop it BEFORE the success handler. Because the function finishes executing after your AJAX call the form will submit while your ajax call is occurring (and by the time your ajax call finishes it is too late).

但是,是的,将return false放在函数末尾.

But yes, put return false at the end of your function.

function SubmitHandler(){
  // Your AJAX call here
  // blah blah blah

  return false;  // Stop form submit
}

如果在成功处理程序中确实很重要,则可以同步执行调用.

If it's really important that it is in the success handler, then you could execute the call synchronously.

这篇关于如何在ajax调用期间停止表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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