jQuery Mobile how to check if button is disabled?

2020-02-24 05:25发布

I disable the button like this on my jQuery Mobile webpage:

$(document).ready(function() {
    $("#deliveryNext").button();
    $("#deliveryNext").button('disable');
});

and i can enable it with

$("#deliveryNext").button('enable');

But how do i check if the button is disabled or enabled?

This command gives "undefined":

$("#deliveryNext").attr('disabled')

Some ideas?

Edit: i find out that $("#deliveryNext").button('disable') only seams to change the style on the button, the clicking works fine after so i need to disable the button some how also.. i tried .attr('disabled', 'disabled') but when i then test .attr('disabled') i get undefined...

Edit2: more about my "real" problem at How to disable a link button in jQuery Mobile?

11条回答
乱世女痞
2楼-- · 2020-02-24 05:48

You can use jQuery.is() function along with :disabled selector:

$("#savematerial").is(":disabled")
查看更多
狗以群分
3楼-- · 2020-02-24 05:52

try :is selector

$("#deliveryNext").is(":disabled")
查看更多
Anthone
4楼-- · 2020-02-24 05:53

Use .prop instead:

$('#deliveryNext').prop('disabled')
查看更多
贼婆χ
5楼-- · 2020-02-24 05:53

I had the same problem and I found this is working:

if ($("#deliveryNext").attr('disabled')) {
  // do sth if disabled
} else {
  // do sth if enabled 
}

If this gives you undefined then you can use if condition also.

When you evaluate undefined it will return false.

查看更多
家丑人穷心不美
6楼-- · 2020-02-24 05:55

To see which options have been set on a jQuery UI button use:

$("#deliveryNext").button('option')

To check if it's disabled you can use:

$("#deliveryNext").button('option', 'disabled')

Unfortunately, if the button hasn't been explicitly enabled or disabled before, the above call will just return the button object itself so you'll need to first check to see if the options object contains the 'disabled' property.

So to determine if a button is disabled you can do it like this:

$("#deliveryNext").button('option').disabled != undefined && $("#deliveryNext").button('option', 'disabled')
查看更多
登录 后发表回答