我有这样的表格:
<div class="form-group">
<label class="checkbox">
<input type="checkbox" name="item1" value="true" checked> Item 1
</label>
</div>
<div class="form-group">
<label class="checkbox">
<input type="checkbox" name="item2" value="true"> Item 1
</label>
</div>
这个JS:
$.(".checkbox input[type='checkbox']").each(function() {
if ($.(this).is(":checked")) {
$.(this).closest(".checkbox").addClass("checked");
$.(this).closest(".radio").addClass("checked");
}
});
$.(".checkbox input[type='checkbox']").bind("change", function() {
if ($.(this).is(":checked")) {
$.(this).closest(".checkbox").addClass("checked");
} else {
$.(this).closest(".checkbox").removeClass("checked");
}
});
如何更改我的JS代码以删除未选中复选框中的checked
属性?
实际上课程发生了变化,但检查的价值仍然存在。
感谢。
我尝试没有成功:
$.(this).prop('checked', true);
和
$.(this).closest(".checkbox > input:checkbox").prop('checked', false);
答案 0 :(得分:0)
使用$.(this).closest(".checkbox").attr("checked", '');
远程检查属性和
$.(this).closest(".checkbox").attr("checked", 'checked');
添加它。
当您使用addClass
时,它会添加一个CSS类,您希望更改属性checked
而不是触及CSS类。
编辑:
$(".checkbox input[type='checkbox']").each(function() {
updateState($(this));
});
$(".checkbox input[type='checkbox']").on("change", function() {
updateState($(this));
});
function updateState($input){
if ($input.is(":checked")) {
$input.closest(".checkbox").addClass("checked");
$input.closest(".radio").addClass("checked");
}
else{
$input.removeAttr('checked');
$input.closest(".checkbox").removeClass("checked");
$input.closest(".radio").removeClass("checked");
}
}