0

In jQuery how could you go about disabling a button and have it re-enable after a few seconds?

I've tried using a .delay(###).attr and .removeattr, but I can't get it to work. From what I've read this is due to .delay made for animations and instead I need to use fadeout?

4

3 回答 3

2
var $btn =  $('input[type="button"]'),
    aFewSeconds = 3000;

$btn.prop({ disabled: true });

setTimeout(function(){
    $btn.prop({ disabled: false});
}, aFewSeconds);

你明白了。

于 2013-10-14T18:36:21.137 回答
1

您可以使用window.setTimeout(func, delay, [param1, param2, ...]);. 这是一个按钮,单击时将被禁用一秒钟,尽管可以使用任何东西来调用此操作:

HTML

<button class='tempDisable'>Click Me</button>

JavaScript (包装在就绪函数中)

$(".tempDisable").click(function () {
    $(this).prop("disabled", true);
    setTimeout(function (el) {
        $(el).prop("disabled", false);
    }, 1000, this);
});

jsFiddle

于 2013-10-14T18:42:57.963 回答
0

我不是 100% 确定这会解决您的问题,但尝试使用$elem.attr('disabled', true)禁用,$elem.attr('disabled', false)再次启用。有些理由$elem.removeAttr('disabled')可能不可靠。

.delay()不过应该不是问题,请发布您的代码。

于 2013-10-14T18:36:59.337 回答