简体   繁体   English

复选框已禁用(Jquery)

[英]Check Checkbox Disabled (Jquery)

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() .我试过.is(':disabled').hasAttr().prop().attr() Any help would be greatly appreciated.任何帮助将不胜感激。

You have to check whether the disabled attribute is true: .attr('disabled') . 您必须检查disabled属性是否为true: .attr('disabled')

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


EDIT: So it seems that .attr is 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/

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')不是。

for me, this works like a charm to disable:对我来说,这就像禁用的魅力:

$("#myCheckBox").prop("disabled", true) 

to enable:启用:

$("#myCheckBox").prop("disabled", false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM