Javascript 复选框已禁用(Jquery)

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

Check Checkbox Disabled (Jquery)

javascriptcheckboxclient-side

提问by user1595357

The purpose is to;
If checkbox is disabled, do nothing.
If checkbox is enabled and checked, set the style of a button.
Here is what I've got so far;

目的是为了;
如果复选框被禁用,则什么都不做。
如果复选框已启用并选中,则设置按钮的样式。
这是我到目前为止所得到的;

 $(document).ready(function (e) {


        $(".checkbox").live("click", function () {

            if ($(this).hasAttribute('disabled')) {
                return false;
            }
            var isAnyChecked;

            $("input[type=checkbox]").each(function () {
                var checkedValue = $(this).attr("checked");
                if (checkedValue == "checked") {
                    isAnyChecked = true;
                }


            });

            if (isAnyChecked) {
                $("#<%= btnConfirm.ClientID %>").css("display", "block");

            } else {
                $("#<%= btnConfirm.ClientID %>").css("display", "none");

            }


        });  });

I've tried .is(':disabled'), .hasAttr(), .prop()and .attr(). Any help would be greatly appreciated.

我试过.is(':disabled'), .hasAttr(),.prop().attr()。任何帮助将不胜感激。

回答by Rodolphe

You have to check whether the disabledattribute is true: .attr('disabled').

您必须检查该disabled属性是否为真:.attr('disabled')

Or, better, use .is(':disabled').---

或者,更好的是,使用.is(':disabled').---



EDIT:So it seems that .attris now deprecated for this use (see http://api.jquery.com/attr/) The preferred way is:

编辑:所以现在似乎.attr已弃用此用途(请参阅http://api.jquery.com/attr/)首选方法是:

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

It has to be noted that this still work as of today:

必须指出的是,这在今天仍然有效:

$('#myCheckbox').is(':disabled')

Try it here: https://jsfiddle.net/Robloche/phaqfrmj/

在这里试试:https: //jsfiddle.net/Robloche/phaqfrmj/

回答by Rostyslav Zhalivtsiv

Also you can try use this:

你也可以尝试使用这个:

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

Works for me to determine if an element is disabled, while others solutions (.disabled, .is(':disabled'), .attr('disabled'), .prop('disabled')) not.

为我工作以确定元素是否被禁用,而其他解决方案 ( .disabled, .is(':disabled'), .attr('disabled'), .prop('disabled')) 不是。