如何在 Javascript 中延迟提交表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4148210/
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
How do I submit a form in Javascript with a delay
提问by milan
I am trying to submit the form automatically with a delay in a chrome extension I am writing and it does not seem to be submitting. Below is my form and javascript:
我正在尝试在我正在编写的 chrome 扩展中延迟自动提交表单,但它似乎没有提交。下面是我的表单和 javascript:
function submitForm() { // submits form
document.getElementById("ismForm").submit();
}
if (document.getElementById("ismForm")) {
setTimeout("submitForm()", 5000); // set timout
}
<form method="post" id="ismForm" name="ismForm" action="http://www.test.com" class="">
<label for="searchBox">Search </label>
<input type="text" id="searchBox" name="q" value=""> <input type="hidden" id="sayTminLength" value="3"><input type="hidden" id="coDomain" value="US"><input class="button" type="submit" id="search.x" name="search.x" value="Search" autocomplete="off">
</form>
采纳答案by icyrock.com
Don't know the context, but it might be that the page has not been loaded yet completely - you might try putting
不知道上下文,但可能是页面尚未完全加载 - 您可以尝试放置
if (document.getElementById("ismForm")) {
setTimeout("submitForm()", 5000); // set timout
}
in body onLoad() event. As another thing, try putting as simple alert before setTimeout and at the start of submitForm() to confirm the timeout is getting fired in the first place.
在主体 onLoad() 事件中。作为另一件事,尝试在 setTimeout 之前和 submitForm() 开始时放置一个简单的警报,以确认首先触发超时。
回答by Icarus
Here's what you need to do (copy and paste):
这是您需要执行的操作(复制和粘贴):
<html>
<head>
<script type="text/javascript">
function submitForm() { // submits form
document.getElementById("ismForm").submit();
}
function btnSearchClick()
{
if (document.getElementById("ismForm")) {
setTimeout("submitForm()", 5000); // set timout
}
}
</script>
</head>
<body>
<form method="post" id="ismForm" name="ismForm" action="http://www.test.com" class="">
<label for="searchBox">Search </label>
<input type="text" id="searchBox" name="q" value=""> <input type="hidden" id="sayTminLength" value="3">
<input type="hidden" id="coDomain" value="US">
<input class="button" onclick="btnSearchClick();" type="button" id="search.x" name="search.x" value="Search" autocomplete="off">
</form>
</body>
</html>
Or, if you want to submit the form after 5 seconds, attach to the windown.onload event the call to btnSearchClick() like so: window.onload=btnSearchClick
或者,如果您想在 5 秒后提交表单,将调用 btnSearchClick() 附加到 windown.onload 事件,如下所示:window.onload=btnSearchClick
回答by Rus Mine
Try this:
尝试这个:
<form method="post" action="yourpage/" id="customForm">
<input type="text" name="input1"/>
<input type="text" name="input2"/>
</form>
<button id="submit">SubmitForm</button><!-- Outside of form -->
<script>
function submitForm() {
document.getElementById("customForm").submit()
}
document.getElementById('submit').onclick = function() {
setTimeout(submitForm, 3000);
}
</script>
回答by Joanna Hauza
Here is the way that works for me:
这是对我有用的方法:
const form = $("#formId");
form.submit(() => {
//some other functions you need to proceed before submit
setTimeout(() => {}, 1200);
return true;
});
Now it will wait 1200 ms before submitting the form. Also, if you return false instead of true, the form won't submit.
现在它将在提交表单之前等待 1200 毫秒。此外,如果您返回 false 而不是 true,表单将不会提交。
回答by Emanuele Del Grande
Solution in jQuery (to avail of expando), easily editable to plain JavaScript:
jQuery 中的解决方案(使用 expando),可轻松编辑为纯 JavaScript:
$('form').eq(0).on('submit', function(e) {
var $form = $(this);
if ($form.data('blocked') !== true) {
// mark the form as blocked
$form.data('blocked', true);
console.log('Scheduling form submit...');
window.setTimeout(function() {
console.log('Submitting!');
$form.submit();
$form.data('blocked', false);
}, 1000);
// disallow submit
return false;
}
});
回答by VarshaSC
Here is one more simple solution:
这是一个更简单的解决方案:
<script type="text/javascript">
function formSubmit(){
document.getElementById("ismForm").submit();
}
window.onload=function(){
window.setTimeout(formSubmit, 5000);
};
</script>