Javascript jQuery:停止提交表单,执行脚本,继续提交表单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6113560/
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-08-23 20:16:26  来源:igfitidea点击:

jQuery: Stop submitting form, perform script, continue submitting form?

javascriptjquery-selectorsjquery

提问by Tuizi

I have a form, and when I submit him I execute multiple script. Here is my code:

我有一个表单,当我提交他时,我会执行多个脚本。这是我的代码:

$("#RequestCreateForm").submit(function (e) {
    if ($("#RequestCreateForm").validate().checkForm() == false) { return; }

    e.preventDefault();

    //many scripts

    //How to continue submitting?
}

Is it possible to continue submitting the form (which is stopped with e.preventDefault();) after //many scripts?

之后是否可以继续提交表单(以 停止e.preventDefault();//many scripts

Thank you

谢谢

采纳答案by Adrian J. Moreno


$("#RequestCreateForm").submit(function (e) {
    if ($("#RequestCreateForm").validate().checkForm() === false) { 
       e.preventDefault(); 
       //form was NOT ok - optionally add some error script in here

       return false; //for old browsers 
    } else{
       //form was OK - you can add some pre-send script in here
    }

    //$(this).submit(); 
    //you don't have to submit manually if you didn't prevent the default event before
}

回答by Chris

When you call $("#RequestCreateForm").submit(), the script will just run through the event handler again, and cause an infinite loop (as Koen pointed out in a comment on the accepted answer). So, you need to remove the event handler before submitting:

当您调用 时$("#RequestCreateForm").submit(),脚本将再次运行事件处理程序,并导致无限循环(正如 Koen 在对已接受答案的评论中指出的那样)。因此,您需要在提交之前删除事件处理程序:

$("#RequestCreateForm").on('submit', function (e) {
    e.preventDefault();
    // do some stuff, and if it's okay:
    $(this).off('submit').submit();
});

The last line needs to be in a conditional statement, otherwise it'll just always happen, and negate your e.preventDefault();at the top.

最后一行需要在条件语句中,否则它总是会发生,并e.preventDefault();在顶部否定您。

回答by Bhanu Krishnan

$("#RequestCreateForm").submit(function (e) {
    if ($("#RequestCreateForm").validate().checkForm() == false) 
    { 
         e.preventDefault();
         return false; 
    }        

    //other scripts

}

回答by Stan Quinn

$("#RequestCreateForm").submit(function (e) {
    if ($("#RequestCreateForm").validate().checkForm() == false) { return; }

    e.preventDefault();

    //many scripts

    // Bypass the jquery form object submit and use the more basic vanilla
    // javascript form object submit

    $("#RequestCreateForm")[0].submit();

}

回答by jave.web

All solutions here are too complicated or lead to javascript error, simpliest and clearestsolution I guess:

这里的所有解决方案都太复杂或导致 javascript 错误,我猜是简单和最清晰的解决方案:

jQuery("#formid").submit(function(e) {
        if( ! (/*check form*/)  ){  //notice the "!"
          e.preventDefault();
          //a bit of your code
        } //else do nothing, form will submit
}); 

回答by Emk

Here is my approach to avoid the infinite loop.

这是我避免无限循环的方法。

  • In the form, I use a "button" with an id (e.g. <input type="button" id="submit" value="submit"/>) to mimic the submit button;
  • In the script I have something like this:
  • 在表单中,我使用带有 id(例如<input type="button" id="submit" value="submit"/>)的“按钮”来模拟提交按钮;
  • 在脚本中,我有这样的事情:
$('#submit').click(function() {
  if(//validation rules is ok)
  {
      $("#RequestCreateForm").submit(); //assuming the form id is #RequestCreateForm
  }
  else
  {
      return false;
  }
});
$('#submit').click(function() {
  if(//validation rules is ok)
  {
      $("#RequestCreateForm").submit(); //assuming the form id is #RequestCreateForm
  }
  else
  {
      return false;
  }
});

回答by Fedir RYKHTIK

To avoid submit loops, an additional variable should be used.

为避免提交循环,应使用附加变量。

var codeExecuted = false;
$('#RequestCreateForm').submit(function(e) {
    ...
    if(!codeExecuted){
        e.preventDefault();
        ...
        functionExecuted = true;
        $(this).trigger('submit');
    }
});

回答by max4ever

return; is the same thing as e.preventDefault();

返回; 与 e.preventDefault() 相同;

try

尝试

$("#RequestCreateForm").trigger('submit');