我有一组复选框(Sub1,Sub2,Sub3)和一个主复选框。如果检查任何子复选框,则必须检查主复选框。如果所有子复选框都未选中,则只有主复选框必须取消选中,如果至少选中一个复选框,则必须选中主复选框。我的代码如下所示。如果取消选中所有子复选框,如何删除选中的复选框?
<!DOCTYPE html>
<html>
<script type='text/javascript' src="jquery-1.10.1.js"></script>
<script type='text/javascript'>
$(window).load(function() {
var chk1 = $("input[type='checkbox'][value='1']");
var chk2 = $("input[type='checkbox'][value='2']");
var chk3 = $("input[type='checkbox'][value='3']");
var chk4 = $("input[type='checkbox'][value='4']");
chk2.on('change', function() {
chk1.prop('checked', this.checked);
});
chk3.on('change', function() {
chk1.prop('checked', this.checked);
});
chk4.on('change', function() {
chk1.prop('checked', this.checked);
});
});
</script>
</head>
<body>
<input type="checkbox" value="1" class="user_name">Main1
<br>
<br>
<br>
<input type="checkbox" value="2" id="one" class="user_name">sub-2
<br>
<input type="checkbox" value="3" id="one" class="user_name">sub-3
<br>
<input type="checkbox" value="4" id="one" class="user_name">sub-4
<br>
</body>
</html>
Hi Tushar,apt answer。如果我有另一组Main2((Sub-5,Sub-6,Sub-7))会怎么样?我们需要为另一组复选框做些什么改变。 –
@GaneshSudarsan你需要分享html结构 –
@GaneshSudarsan http://jsfiddle.net/arunpjohny/cpncz12L/2/? –