我有以下代码:

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秒钟后提交。

我尝试将其与setTimeout()一起使用,但它根本不起作用。我不想与此一起使用jQuery,能否为我提供如何执行此操作的代码。

最佳答案

在表单中添加一个ID:

<form id="whatever">


然后,在您的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;
}

09-20 06:47