最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

When button is pressed wait 3 seconds then submit javascript - Stack Overflow

programmeradmin4浏览0评论

How do i make a Javascript that will make so when i press my submit button it will change the name of the button to something like this "Loading..." and after 3 sec the button should be pressed

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

It will act as an "spam filter" for humans..

How do i make a Javascript that will make so when i press my submit button it will change the name of the button to something like this "Loading..." and after 3 sec the button should be pressed

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

It will act as an "spam filter" for humans..

Share Improve this question asked Nov 30, 2012 at 22:05 Hannes Brolin LagerstedtHannes Brolin Lagerstedt 874 silver badges12 bronze badges 3
  • That's not a great way to prevent spam... – Brad Commented Nov 30, 2012 at 22:07
  • Define "spam filter". If you are worrying about bots this will fail, because they will not have JS enabled in most cases. – PeeHaa Commented Nov 30, 2012 at 22:08
  • You have to use a timer w3schools./js/js_timing.asp the on mouse down will put a flag somewhere and start the timer, on mouse up will remove this flag and stop the timer. If once the timer is fired the flag is present, then you know that you pressed the mouse all the time long. – Flavien Volken Commented Nov 30, 2012 at 22:08
Add a ment  | 

2 Answers 2

Reset to default 7

One possible solution is to set submit event for form element in the following way:

HTML:

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

JavaScript:

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

Another way is to set the event dynamically (tribute to the kittens!):

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/spWDk/

You can try the below solution:

<form action="https://google." 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>
发布评论

评论列表(0)

  1. 暂无评论