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

发布于 2024-11-09 11:26:16 字数 369 浏览 0 评论 0原文

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

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

    e.preventDefault();

    //many scripts

    //How to continue submitting?
}

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

谢谢

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?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

揽月 2024-11-16 11:26:16

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

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

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

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").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.

酒儿 2024-11-16 11:26:16

$("#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
}

$("#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
}
假装不在乎 2024-11-16 11:26:16
$("#RequestCreateForm").submit(function (e) {
    if ($("#RequestCreateForm").validate().checkForm() == false) 
    { 
         e.preventDefault();
         return false; 
    }        

    //other scripts

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

    //other scripts

}
舂唻埖巳落 2024-11-16 11:26:16

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

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

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

jQuery("#formid").submit(function(e) {
        if( ! (/*check form*/)  ){  //notice the "!"
          e.preventDefault();
          //a bit of your code
        } //else do nothing, form will submit
}); 
歌入人心 2024-11-16 11:26:16
$("#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();

}
$("#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();

}
孤独患者 2024-11-16 11:26:16

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

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

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');
    }
});
月隐月明月朦胧 2024-11-16 11:26:16

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

  • 在表单中,我使用带有 id 的“按钮”(例如 )来模仿提交按钮;
  • 在脚本中我有这样的内容:
$('#submit').click(function() {
  if(//验证规则没问题)
  {
      $("#RequestCreateForm").submit(); //假设表单id是#RequestCreateForm
  }
  别的
  {
      返回假;
  }
});

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:
$('#submit').click(function() {
  if(//validation rules is ok)
  {
      $("#RequestCreateForm").submit(); //assuming the form id is #RequestCreateForm
  }
  else
  {
      return false;
  }
});
孤独患者 2024-11-16 11:26:16

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

尝试

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

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

try

$("#RequestCreateForm").trigger('submit');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文