如何在表单提交前等待3秒? [英] How to wait 3 seconds before form submits?
本文介绍了如何在表单提交前等待3秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
function checkAmount() {
var PayField = document.getElementById('paymentamount');
var Pound = document.getElementById('pound');
if (PayField.value == "") {
PayField.style.border = '1px #F00 solid';
Pound.style.color = '#F00';
alert('You need to enter an amount that you wish to donate');
return false;
}
return true;
}
当用户输入有效金额并点击付款按钮时,表单应该等待3秒然后等待3秒后提交。
When a user types a valid amount and clicks the pay button, the form should wait 3 seconds then submits after waiting 3 seconds.
我尝试使用 setTimeout()
,但是它根本不起作用。我不想用jQuery,你能给我一个代码怎么做。
I've tried using setTimeout()
with it, but it doesn't work at all. I don't want to use jQuery with this, can you provide me a code how to do it.
推荐答案
添加一个ID到您的表格:
Add an ID to your form:
<form id="whatever">
然后,在你的JavaScript中:
Then, in your JavaScript:
var waited = false;
function checkAmount()
{
var PayField = document.getElementById('paymentamount');
var Pound = document.getElementById('pound');
if (PayField.value == "")
{
PayField.style.border = '1px #F00 solid';
Pound.style.color = '#F00';
alert('You need to enter an amount that you wish to donate');
return false;
}
if (waited)
{
return true;
}
waited = true;
setTimeout(function() { document.getElementById('whatever').submit() }, 3000);
return false;
}
这篇关于如何在表单提交前等待3秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文