Ericpoon
Ericpoon

Reputation: 267

jQuery to disable button not work

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.

Upvotes: 19

Views: 43234

Answers (3)

zhujy_8833
zhujy_8833

Reputation: 571

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

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

Upvotes: 3

Adam Rackis
Adam Rackis

Reputation: 83356

You'll want to use the prop function instead of attr:

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

and

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

EDIT

OP was using jQuery pre 1.6.1, which was why attr for disabled wasn't working quite right (for older IE). While prop is preferred for disabled, post 1.6.1 attr should (and does) work as well.

Upvotes: 44

Wes Crow
Wes Crow

Reputation: 2967

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

Upvotes: 5

Related Questions