简体   繁体   English

jquery复选框已选中/未选中

[英]jquery checkbox checked / unchecked

I am trying to do some checkbox manipulation with jquery. 我试图用jquery做一些复选框操作。 I have a modal popup and checkbox in it and I am using jquery to manipulate the checked mode. 我有一个模态弹出窗口和复选框,我使用jquery来操作选中的模式。 There is a variable which has either True or False, so if its True then check otherwise uncheck. 有一个变量有True或False,所以如果是True,则检查否则取消选中。 But, in my case even when the value is False, the checkbox still stays checked. 但是,在我的情况下,即使值为False,复选框仍然保持选中状态。 This is the code I am using: 这是我正在使用的代码:

$(document).on("click", ".open-EditCC", function () {            
            var life = $(this).data('life');           
            $('#<%=chkLife.ClientID%>').attr('checked', life);
            $('#editCC').modal('show');
          });

life variable gets either True or False but all the time the checkbox is checked, I set a breakpoint I can see that the value is False. life变量得到True或False,但是一直选中复选框,我设置了一个断点,我可以看到该值为False。 Any idea what am I doing wrong? 知道我做错了什么吗? Thanks in advance, Laziale 提前谢谢,Laziale

The value of the checked attribute is "checked" or the checked attribute is not present so use this: 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');
}

I guess it should be enough to remove attribute when neccessary. 我想在必要时删除属性应该足够了。

if(life){
 $('#<%=chkLife.ClientID%>').attr('checked', 'checked');
}
else

{
$('#<%=chkLife.ClientID%>').removeRttr('checked');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM