如何延迟提交Javascript表单
我正在尝试自动提交表单,但在我正在编写的chrome扩展中有延迟,而且它似乎没有提交。以下是我的表单和javascript:如何延迟提交Javascript表单,javascript,forms,submit,delay,Javascript,Forms,Submit,Delay,我正在尝试自动提交表单,但在我正在编写的chrome扩展中有延迟,而且它似乎没有提交。以下是我的表单和javascript: function submitForm() { // submits form document.getElementById("ismForm").submit(); } if (document.getElementById("ismForm")) { setTimeout("submitForm()", 5000); // set timout
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>
函数submitForm(){//提交表单
document.getElementById(“ISMGorm”).submit();
}
if(document.getElementById(“ISMGorm”)){
setTimeout(“submitForm()”,5000);//设置超时
}
搜寻
不知道上下文,但可能是页面尚未完全加载-您可以尝试放置
if (document.getElementById("ismForm")) {
setTimeout("submitForm()", 5000); // set timout
}
在body onLoad()事件中。另一件事是,在setTimeout之前和submitForm()开始时,尝试将As simple alert放在首位,以确认超时正在被触发。以下是您需要执行的操作(复制和粘贴):
函数submitForm(){//提交表单
document.getElementById(“ISMGorm”).submit();
}
函数btnSearchClick()
{
if(document.getElementById(“ISMGorm”)){
setTimeout(“submitForm()”,5000);//设置超时
}
}
搜寻
或者,如果您想在5秒钟后提交表单,请将对btnSearchClick()的调用附加到windown.onload事件,如:window.onload=btnSearchClick
<script type="text/javascript">
function formSubmit(){
document.getElementById("ismForm").submit();
}
window.onload=function(){
window.setTimeout(formSubmit, 5000);
};
</script>
函数formSubmit(){
document.getElementById(“ISMGorm”).submit();
}
window.onload=function(){
设置超时(formSubmit,5000);
};
试试这个:
<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>
提交表格
函数submitForm(){
document.getElementById(“customForm”).submit()
}
document.getElementById('submit')。onclick=function(){
setTimeout(submitForm,3000);
}
以下是适合我的方法:
const form = $("#formId");
form.submit(() => {
//some other functions you need to proceed before submit
setTimeout(() => {}, 1200);
return true;
});
现在它将在提交表单之前等待1200毫秒。此外,如果返回false而不是true,则表单将不会提交。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;
}
});
由于某些原因,代码没有进入submitForm()函数。我通过在submitForm()函数的开头添加一个警报来测试这一点。我无法更改表单。我正在为Chrome编写一个扩展。由于某种原因,代码没有进入submitForm()函数。我通过在submitForm()函数的开头添加一个警报来测试这一点。
$('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;
}
});