Javascript jQuery Mobile 如何检查按钮是否被禁用?

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

jQuery Mobile how to check if button is disabled?

javascriptjqueryjquery-uijquery-mobile

提问by RickardP

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

我在我的 jQuery Mobile 网页上禁用了这样的按钮:

$(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...

编辑:我发现 $("#deliveryNext").button('disable') 只接缝改变按钮上的样式,点击后工作正常,所以我需要禁用按钮一些如何也......我试过。 attr('disabled', 'disabled') 但是当我然后测试 .attr('disabled') 我得到未定义...

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

Edit2:在如何禁用 jQuery Mobile 中的链接按钮?

回答by Vivek

try :isselector

尝试:is选择器

$("#deliveryNext").is(":disabled")

回答by kinakuta

Use .prop instead:

使用 .prop 代替:

$('#deliveryNext').prop('disabled')

回答by lancscoder

Try

尝试

$("#deliveryNext").is(":disabled")

The following code works for me:

以下代码对我有用:

<script type="text/javascript">
    $(document).ready(function () {
        $("#testButton").button();
        $("#testButton").button('disable');
        alert($('#testButton').is(':disabled'));
    });
</script>
<p>
    <button id="testButton">Testing</button>
</p>

回答by Mina Gabriel

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 ifcondition also.

如果这给您未定义,那么您也可以使用if条件。

When you evaluate undefinedit will return false.

当你评估undefined它会返回false

回答by Rostyslav Zhalivtsiv

Faced with the same issue when trying to check if a button is disabled. I've tried a variety of approaches, such as btn.disabled, .is(':disabled'), .attr('disabled'), .prop('disabled'). But no one works for me.

尝试检查按钮是否被禁用时遇到同样的问题。我尝试了多种方法,例如btn.disabled, .is(':disabled'), .attr('disabled'), .prop('disabled')。但没有人为我工作。

Some, for example .disabledor .is(':disabled')returned undefinedand other like .attr('disabled')returned the wrong result - falsewhen the button was actually disabled.

一些,例如.disabled.is(':disabled')返回undefined和其他类似.attr('disabled')返回错误的结果 -false当按钮实际上被禁用时。

But only one technique works for me: .is('[disabled]')(with square brackets).

但是,只有一个技术工作对我来说.is('[disabled]')方括号)。

So to determine if a button is disabled try this:

因此,要确定按钮是否被禁用,请尝试以下操作:

$("#myButton").is('[disabled]');

回答by Kaido

Try

尝试

$("#deliveryNext").is('[disabled]');

回答by Hymankorbin

http://jsfiddle.net/8gfYZ/11/Check here..

http://jsfiddle.net/8gfYZ/11/检查这里..

$(function(){

    $('#check').click(function(){
        if( $('#myButton').prop('disabled') ) { 
            alert('disabled'); 
            $('#myButton').prop('disabled',false);
        } 
        else {
            alert('enabled');
            $('#myButton').prop('disabled',true);
        }
    });
});

回答by Tom Wadley

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

要查看在jQuery UI按钮上设置了哪些选项,请使用:

$("#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')

回答by John Archer

What worked for me was this:

对我有用的是:

$("#testButton").hasClass('ui-state-disabled')

But I like tomwadley's answer a lot more.

但我更喜欢 tomwadley 的回答。

Nevertheless, the is(':disabled') code does also not work for me which should be the best version. Don't know, why it does not work. :-(

尽管如此, is(':disabled') 代码对我来说也不起作用,这应该是最好的版本。不知道,为什么它不起作用。:-(

回答by Siva Ragu

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

您可以将jQuery.is()函数与:disabled选择器一起使用:

$("#savematerial").is(":disabled")