js 复选框 全选都选 如果某一个子复选框没选中 则全选按钮不选中
<!DOCTYPE HTML>
<html>
<head>
<meta charset=UTF-8>
<title>YuGiOh</title>
<style type="text/css">
</style>
<script type="text/javascript">
onload = function ()
{
var obox = document.getElementById ("box");
var odiv = document.getElementById ("div1");
var ach = odiv.getElementsByTagName ("input");
//设置全选按钮选中的时候,遍历选中所有子复选框
obox.onclick = function ()
{
for ( var i = 0; i < ach.length; i++)
{
ach[i].checked = this.checked;
}
}
//子复选框有一个未选中时,去掉全选按钮的选中状态
for ( var i = 0; i < ach.length; i++)
{
ach[i].onclick = function ()
{
if ( !this.checked )
{
obox.checked = false;
}
};
}
}
</script>
</head>
<body>
<label><input id="box" type="checkbox" />全选</label>
<div id="div1">
<label><input type="checkbox" />1</label><br />
<label><input type="checkbox" />2</label><br />
<label><input type="checkbox" />3</label><br />
<label><input type="checkbox" />4</label><br />
</div>
</body>
</html>