使用 jQuery 禁用复选框

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

disabling checkbox using jQuery

jquery

提问by beebek

I am disabling the ID checkbox using following jquery

我正在使用以下 jquery 禁用 ID 复选框

jQuery

jQuery

$('.ColVis_collection button').first().find('input').attr("disabled", true);

html

html

This code is inside another button, which when clicked gets activated like a dropdown.

此代码位于另一个按钮内,单击该按钮时会像下拉菜单一样被激活。

<div class="ColVis_collection TableTools_collection" style="display: block; position: absolute; opacity: 1; top: 102px; left: 274px; width: 449px;">
    <button class="ColVis_Button TableTools_Button" style="width: 449px;">
        <span>
            <span class="ColVis_radio">
                <input type="checkbox" checked="checked">
            </span>
            <span class="ColVis_title">
                <span>
                    id
                </span>
            </span>
        </span>
    </button>
    <button class="ColVis_Button TableTools_Button" style="width: 449px;">
        <span>
            <span class="ColVis_radio">
                <input type="checkbox" checked="checked">
            </span>
            <span class="ColVis_title">
                <span>
                    name
                </span>
            </span>
        </span>
    </button>
    .
    .
    .
</div>

The first button with span id gets disabled using the jquery and blurred showing that it is disabled but it can be clicked i.e. checked/unchecked. Am I missing something?

使用 jquery 禁用第一个带有 span id 的按钮并模糊显示它已禁用但可以单击,即选中/取消选中。我错过了什么吗?

Edit:

编辑:

I am using jQuery datatables colvis feature, which lets you hide/unhide the table columns

我正在使用 jQuery 数据表 colvis 功能,它可以让您隐藏/取消隐藏表列

As you can see in this image, the checkbox seems disabled but can be checked/uncheked

正如您在此图像中看到的,复选框似乎已禁用,但可以选中/取消选中

Image

图片

回答by Arun P Johny

Use .prop()instead of .attr()to set the disabled property

使用.prop()而不是.attr()来设置禁用属性

$('.ColVis_collection button').first().find('input[type="checkbox"]').prop("disabled", true);

Demo: Fiddle

演示:小提琴

Attributes vs. Properties

属性与属性

回答by Sai Avinash

try some thing like this:

尝试这样的事情:

 $("input").prop('disabled', true);

回答by beebek

Didn't figured out what happened. May be I'll look later. Ended up hiding the first button i.e.

没搞清楚怎么回事。可能是我稍后看。最终隐藏了第一个按钮,即

$('.ColVis_collection button').first().attr("hidden", true);

回答by Pat Dobson

Try adding a read only attribute as well:

也尝试添加只读属性:

$('.ColVis_collection button').first().find('input').attr("disabled", true).attr("readonly", true);

That might sort you out . . .

那可能会让你解决问题。. .