2014-02-18 119 views
0

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

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

上面的代码是不是让我的复选框被禁用,而我能够检查并取消选中该复选框。
我唯一想做的事情是使复选框被禁用,如果我从后端加载的复选框的值被选中,那么它不应该影响它的状态。我也想禁用复选框的文本。

请注意我已经生成动态的复选框,这里是它的代码:

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; 
+0

分享你的HTML样本 –

+0

你不需要':checkbox'和'[类型= “复选框”]'在一起。其中之一就足够了。 – techfoobar

+0

@ArunPJohny你可以让我知道你想要哪个输入文本html,并且我正在动态生成复选框... – HarshSharma

回答

0

请尝试以下

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

请检查我现在更新的问题.. – HarshSharma

+0

动态生成后执行下面的代码 $( '输入[类型= “复选框”]')每个(函数(){ \t \t \t $(本).prop( “禁用”,真); \t \t}); \t \t 或把它准备funcction内部 $(文件)。就绪(函数(){ \t \t $( '输入[类型= “复选框”]')。每个(函数(){ \t \t \t $(this).prop(“disabled”,true); \t \t}); }); – Sarath

0

你正在做的错$('input:checkbox[type="checkbox"]')

你应该这样使用:

$('input:checkbox')只有

,或者

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


,并使用道具方法:

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

还要注意你在做什么错在这里的属性的方法:

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

这应该是这样的:

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

请检查我现在更新的问题 – HarshSharma

0

使用

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

而且

$('input[type=checkbox]') 

正如上述用户萨拉(纠正一点点):

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

请检查我现在更新的问题 – HarshSharma