【发布时间】:2015-10-07 08:41:10
【问题描述】:
我有一组复选框(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>
【问题讨论】:
标签: javascript jquery html checkbox