按下按钮等待3秒然后提交javascript

时间:2012-11-30 22:05:38

标签: javascript

当我按下提交按钮时,如何制作一个Javascript,它会将按钮的名称更改为“Loading ...”,3秒后按下按钮

<input type="Submit" name="submitNew" id="submitNew" value="Send">

它将充当人类的“垃圾邮件过滤器”..

2 个答案:

答案 0 :(得分:6)

一种可能的解决方案是按以下方式为submit元素设置form事件:

HTML:

<form onsubmit="return formSubmit(this);">

JavaScript的:

function formSubmit(form) {
    document.getElementById("submitNew").value = "Loading...";
    setTimeout(function() {
        form.submit();
    }, 3000);  // 3 seconds
    return false;
}

另一种方法是动态设置事件(向小猫致敬!):

window.onload = function() {
    document.getElementById("formID").onsubmit = function() {
        var form = this;
        document.getElementById("submitNew").value = "Loading...";
        setTimeout(function() {
            form.submit();
        }, 3000); // 3 seconds
        return false;
    };
};​

DEMO: http://jsfiddle.net/spWDk/

答案 1 :(得分:0)

您可以尝试以下解决方案:

<form action="https://google.com" method="post" name="simulation" id="simulation">
    <input type="text" name="nom" id="nom"/>
    <input type="text" name="prenom" id="prenom"/>
    <input type="submit" value"Submit">
</form>
<script>
$("#simulation").on('submit', function (e) {
    console.log("Submitted");
    //stop form submission
    e.preventDefault();
    //Do your custom work.
    setTimeout(function(){ document.getElementById('simulation').submit(); }, 5000);
});
</script>