复选框和复选框文本禁用

时间:2014-02-18 06:35:18

标签: jquery

我有一个复选框,我想禁用复选框的禁用文本,但我没有这样做。我有以下代码:

$('input:checkbox[type="checkbox"]').each(function () {
   $(this).attr("disabled", true).attr("readonly", true);
});

上面的代码没有禁用我的复选框,而是我可以选中并取消选中复选框 我唯一想做的是禁用复选框,如果检查我从后端加载的复选框的值,那么它不应该影响它的状态。我还想禁用复选框的文本。

请注意我已动态生成Checkbox,以下是代码:

if (cellvalue == "True") {
    html = "<input type='checkbox' class='checkboxUser' id='checkbox" + rowid + "' checked/>"
}
else {
    html = "<input type='checkbox' class='checkboxUser' id='checkbox" + rowid + "'/>"
}
return html;

3 个答案:

答案 0 :(得分:0)

请尝试以下

$('input:checkbox[type="checkbox"]').each(function () {
   $(this).prop("disabled", true);
});

答案 1 :(得分:0)

$('input:checkbox[type="checkbox"]')

你做错了

你应该这样使用:

$('input:checkbox')

,或者

$('input[type="checkbox"]')


并使用prop方法:

$('input[type="checkbox"]').each(function () {
   $(this).prop("disabled", true);
   $(this).prop("readonly", true);
});

另请注意您在这里做错的属性方法:

$(this).attr("disabled",true).attr("readonly",true); //因为this.attr的attr无效

这应该是这样的:

$(this).attr("disabled",true);
$(this).attr("readonly",true);

答案 2 :(得分:0)

使用

prop("disabled", true)代替attr(...)

$('input[type=checkbox]')

正如所说的用户Sarath(稍作修正):

$('input[type="checkbox"]').each(function () {
   $(this).prop("disabled", true);
});
相关问题