jQuery:停止提交表单,执行脚本,继续提交表单?
我有一个表单,当我提交他时,我执行多个脚本。这是我的代码:
$("#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
当您调用 $("#RequestCreateForm").submit() 时,脚本将再次运行事件处理程序,并导致无限循环(正如 Koen 在对已接受答案的评论中指出的那样) )。因此,您需要在提交之前删除事件处理程序:
最后一行需要位于条件语句中,否则它总是会发生,并否定顶部的
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:The last line needs to be in a conditional statement, otherwise it'll just always happen, and negate your
e.preventDefault();
at the top.这里的所有解决方案都太复杂或导致 javascript 错误,我猜最简单和最清晰解决方案:
All solutions here are too complicated or lead to javascript error, simpliest and clearest solution I guess:
为了避免提交循环,应该使用一个附加变量。
To avoid submit loops, an additional variable should be used.
这是我避免无限循环的方法。
)来模仿提交按钮;
Here is my approach to avoid the infinite loop.
<input type="button" id="submit" value="submit"/>
) to mimic the submit button;返回;与 e.preventDefault() 相同;
尝试
return; is the same thing as e.preventDefault();
try