[英]jquery checkbox checked / unchecked
我試圖用jquery做一些復選框操作。 我有一個模態彈出窗口和復選框,我使用jquery來操作選中的模式。 有一個變量有True或False,所以如果是True,則檢查否則取消選中。 但是,在我的情況下,即使值為False,復選框仍然保持選中狀態。 這是我正在使用的代碼:
$(document).on("click", ".open-EditCC", function () {
var life = $(this).data('life');
$('#<%=chkLife.ClientID%>').attr('checked', life);
$('#editCC').modal('show');
});
life變量得到True或False,但是一直選中復選框,我設置了一個斷點,我可以看到該值為False。 知道我做錯了什么嗎? 提前謝謝,Laziale
checked屬性的值為"checked"
或者checked屬性不存在,請使用:
// This is assuming life has the string value of "True" or "False"
// if it's a boolean change to if (life)
if (life === 'True') {
$('#<%=chkLife.ClientID%>').attr('checked', 'checked');
} else {
$('#<%=chkLife.ClientID%>').removeAttr('checked');
}
我想在必要時刪除屬性應該足夠了。
if(life){
$('#<%=chkLife.ClientID%>').attr('checked', 'checked');
}
else
{
$('#<%=chkLife.ClientID%>').removeRttr('checked');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.