javascript JS /JQuery 表单提交延迟?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5100726/
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
JS /JQuery Form Submit Delay?
提问by jeremycollins
I've implemented this 'locksubmit' plugin http://blog.leenix.co.uk/2009/09/jquery-plugin-locksubmit-stop-submit.htmlwhere it changes the display state of the button to disabled. I then want the form to be delayed by a few seconds before posting to the form URL.
我已经实现了这个“locksubmit”插件http://blog.leenix.co.uk/2009/09/jquery-plugin-locksubmit-stop-submit.html,它将按钮的显示状态更改为禁用。然后我希望表单在发布到表单 URL 之前延迟几秒钟。
What would I have to add in or modify to delay the form "post" once the user has clicked the submit button?
一旦用户单击提交按钮,我必须添加或修改什么以延迟表单“发布”?
Thanks!
谢谢!
回答by David Tang
Cancel the default form submit behaviour, and start a timeout at the same time:
取消默认的表单提交行为,同时开始超时:
$('form').submit(function (e) {
var form = this;
e.preventDefault();
setTimeout(function () {
form.submit();
}, 1000); // in milliseconds
});
This should be compatible with the locksubmit plugin.
这应该与 locksubmit 插件兼容。
See a demo here: http://jsfiddle.net/7GJX6/
在此处查看演示:http: //jsfiddle.net/7GJX6/
回答by philwinkle
I think this is what you're looking for - modify lockSubmit()as such:
我认为这就是你正在寻找的 - 修改lockSubmit()如下:
jQuery(':submit').lockSubmit(function(){
setTimeout(5e3); // fancy 5 seconds
});
And with options object:
并使用选项对象:
jQuery(':submit').lockSubmit({
submitText: "Please wait",
onAddCSS: "submitButtons",
onClickCSS: "submitButtonsClicked"
},function(){
setTimeout(5e3);
}));
Basically javascript has to wait for all arguments to be resolved before executing the body. By setting timeout during the argument we delay the return.
基本上 javascript 必须在执行主体之前等待所有参数都被解析。通过在参数期间设置超时,我们延迟了返回。

