问题:
This question already has an answer here: 这个问题在这里已有答案:
- Setting “checked” for a checkbox with jQuery? 使用jQuery为复选框设置“选中”? 40 answers 40个答案
I have some input text fields in my page and am displaying their values using JavaScript. 我的页面中有一些输入文本字段,并使用JavaScript显示其值。
I am using .set("value","")
function to edit the value, add an extra checkbox field, and to pass a value. 我使用.set("value","")
函数来编辑值,添加一个额外的复选框字段,并传递一个值。
Here I want to check that if value == 1
, then this checkbox should be checked. 在这里,我想检查如果value == 1
,则应选中此复选框。 Otherwise, it should remain unchecked. 否则,它应该保持未选中状态。
I did this by using two divs, but I am not feeling comfortable with that, is there any other solution? 我通过使用两个div来做到这一点,但我对此感到不舒服,还有其他解决方案吗?
if(value == 1) {
$('#uncheck').hide();
$('#check').show();
} else{
$('#uncheck').show();
$('#check').hide();
}
解决方案:
参考一: https://stackoom.com/question/1B5sM/使用jquery检查-取消选中复选框-重复参考二: https://oldbug.net/q/1B5sM/check-uncheck-checkbox-using-jquery-duplicate
来源:oschina
链接:https://my.oschina.net/u/3797416/blog/4411814