2014-02-18 118 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