javascript 使用 jQuery 在短时间内禁用提交按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7929313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:45:32  来源:igfitidea点击:

Disable submit button for a short period of time with jQuery?

javascriptjqueryform-submittime-limiting

提问by Akos

[I am quite new to jQuery so don't blame me if I get something wrong]

[我对 jQuery 很陌生,所以如果我做错了什么不要怪我]

I have been browsing questions here on SO about: "Disable submit button after click". OK there are loads of these stuff around, but I couldn't find out how to disable it for a limited time. e.g. 20 secs.

我一直在浏览有关以下内容的问题:“单击后禁用提交按钮”。好吧,周围有很多这些东西,但我无法找到如何在有限的时间内禁用它。例如 20 秒。

Maybe I am the idiot but how?

也许我是个白痴,但怎么办?

[I've only got a simple html form]

[我只有一个简单的html表单]

回答by karim79

var enableSubmit = function(ele) {
    $(ele).removeAttr("disabled");
}

$("#submit").click(function() {
    var that = this;
    $(this).attr("disabled", true);
    setTimeout(function() { enableSubmit(that) }, 1000);
});

Demo: http://jsfiddle.net/pHxF2/2/

演示:http: //jsfiddle.net/pHxF2/2/

回答by I_Literally_Cannot

I know its been several years since this question was answered, but I had a bit of a problem getting this to work. I got something similar to work for me:

我知道自从回答这个问题以来已经有好几年了,但是我在让它起作用时遇到了一些问题。我得到了类似的东西对我来说有效:

var enableBtn = function() {
        $('.btn').attr("disabled", false);
        }
        $('.second').click(function (){
            var that = this;
            $('.second').attr("disabled", true);
            setTimeout(function() { enableBtn(that) }, 1500);
        });
        $('.main').click(function (){
            var that = this;
            $('.main').attr("disabled", true);
            setTimeout(function() { enableBtn(that) }, 1500);
        });
        $('.disableall').click(function (){
            var that = this;
            $('.btn').attr("disabled", true);
            setTimeout(function() { enableBtn(that) }, 1500);
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" class="btn main" value="disable this">
<input type="button" class="btn second" value="disable this">
<input type="button" class="btn disableall" value="disable all">