jQuery jQuery禁用按钮不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8707423/
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-08-27 10:55:59  来源:igfitidea点击:

jQuery to disable button not work

jqueryhtml

提问by Ericpoon

I used to bind event to my button like :

我曾经将事件绑定到我的按钮,例如:

$("input[name=add]").live("click",function(){...});

But I got another function to make them "enable" and "disabled" like :

但是我有另一个功能可以使它们“启用”和“禁用”,例如:

RefreshBox() {

    $("input[name=add]").each(function(){
        if(...)
            $(this).attr("disabled","disabled");
        else
            $(this).attr("disabled","");
    })

}

But in fact that it doesn't work as I expected. Buttons are still can be click and the event still works smoothly.

但事实上,它并没有像我预期的那样工作。按钮仍然可以单击并且事件仍然可以顺利运行。

PS: jQuery 1.4.1 Browser: IE8 , after button disabled still can work. Browser: Chrome, disabled button success.

PS: jQuery 1.4.1 浏览器: IE8 , 按钮禁用后仍然可以工作。浏览器:Chrome,禁用按钮成功。

回答by Adam Rackis

You'll want to use the propfunction instead of attr:

你会想要使用prop函数而不是attr

$(this).prop("disabled", true);

and

$(this).prop("disabled", false);

EDIT

编辑

OP was using jQuery pre 1.6.1, which was why attrfor disabledwasn't working quite right (for older IE). While propis preferred for disabled, post 1.6.1 attrshould (and does) work as well.

OP 使用 jQuery 1.6.1 之前的版本,这就是为什么attrfordisabled不能正常工作(对于较旧的 IE)。虽然prop是首选disabled,但 1.6.1 后attr应该(并且确实)也可以工作。

回答by Wes Crow

Try $("input[type=submit]").removeAttr("disabled");to enable the button.

尝试$("input[type=submit]").removeAttr("disabled");启用该按钮。

回答by zhujy_8833

You can set the 'disabled' attribute to false by using:

您可以使用以下方法将 'disabled' 属性设置为 false:

$("input[name='add']").attr("disabled","disabled");