javascript 如何在通过 .ajaxForm() 提交表单之前执行一些操作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25970071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:18:59  来源:igfitidea点击:

how to perform some action before submit form via .ajaxForm()?

javascriptjqueryhtmlajaxform

提问by Uday A. Navapara

i am using ajaxForm() frame work to send my data without reloading my page.

我正在使用 ajaxForm() 框架工作来发送我的数据而无需重新加载我的页面。

    $('#ReplayForm').ajaxForm({ 

      success : function(data){
             alert("Success");
       }   
    });  

now, i want to check some condition before submitting the form and if the condition is false then stop submission else continue.

现在,我想在提交表单之前检查一些条件,如果条件为假,则停止提交,否则继续。

is there any solution to do this work or is there any way buy which i can perform this operation. Thanks in advance.

是否有任何解决方案可以完成这项工作,或者有什么方法可以购买我可以执行此操作的方法。提前致谢。

回答by Ashish Vaghasiya

Yes, definatly you can handle this situation. you have to call beforesubmit method for this let see one example

是的,您绝对可以处理这种情况。你必须为此调用 beforesubmit 方法让我们看一个例子

$('#ReplayForm').ajaxForm({ 
         beforeSubmit : function(arr, $form, options){
             if("condition is true")
             {
                return true; //it will continue your submission.
             }
             else
             {
                               return false; //ti will stop your submission.
             }

          },
          success : function(data){
              endLoading();
              if(data.result=="success")            
              {
                  showSuccessNotification(data.notification);
              } 
              else
              {
                  showErrorNotification(data.notification);
              }
           }
   });  

回答by Arun P Johny

You can use the beforeSubmitoption

您可以使用beforeSubmit选项

$('#ReplayForm').ajaxForm({
    beforeSubmit: function (arr, $form, options) {
        //check your conditions and return false to prevent the form submission
        if (!valid) {
            return false;
        }
    },
    success: function (data) {
        alert("Success");
    }
});

回答by Edward

Use beforeSendoption in JQuery AJAX framework, if the test fails return falseshould do it.

beforeSend在 JQuery AJAX 框架中使用选项,如果测试失败return false应该这样做。

$('#ReplayForm').ajaxForm({ 

  success : function(data){
         alert("Success");
  },
  beforeSend: function() {
      if(!myFunc()) {
          return false;
      }
  }
});