Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery to disable button not work

Tags:

html

jquery

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.

like image 850
Ericpoon Avatar asked Jan 03 '12 02:01

Ericpoon


People also ask

How do I make a button disabled?

The disabled attribute is a boolean attribute. When present, it specifies that the button should be disabled. A disabled button is unusable and un-clickable. The disabled attribute can be set to keep a user from clicking on the button until some other condition has been met (like selecting a checkbox, etc.).

How do I set disabled property in JQuery?

Projects In JavaScript & JQuery We can easily disable input box(textbox,textarea) using disable attribute to “disabled”. $('elementname'). attr('disabled','disabled'); To enable disabled element we need to remove “disabled” attribute from this element.

How disable submit button until form is filled JQuery?

1.1 To disable a submit button, you just need to add a disabled attribute to the submit button. $("#btnSubmit"). attr("disabled", true); 1.2 To enable a disabled button, set the disabled attribute to false, or remove the disabled attribute.


2 Answers

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.

like image 54
Adam Rackis Avatar answered Oct 02 '22 03:10

Adam Rackis


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

like image 36
Wes Crow Avatar answered Oct 02 '22 01:10

Wes Crow