求教:想用JS实现一个简单的复选框全选效果,为何实现不了?
想用JS实现一个简单的复选框全选效果,代码如下,可点了全选框后没有反应,哪里错了?请指点!谢谢!
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>复选框全选效果</title>
<script type="text/javascript">
function checkAll()
{
var tBox=document.getElementByName("box");
for(var i=0;i<tBox.length;i++)
{
if(document.getElementById("all").checked==true)
{
tBox[i].checked=true;
}
}
}
</script>
</head>
<body>
<input type="checkbox" name="box" value="1" />苹果<br />
<input type="checkbox" name="box" value="2" />三星<br />
<input type="checkbox" name="box" value="3" />诺基亚<br />
<input type="checkbox" name="box" value="4" />索尼<br />
<input type="checkbox" id="all" value="5" onclick="checkAll()" />全选
</body>
</html>
------解决方案--------------------
C# code
<script type="text/javascript">
function checkAll() {
var tBox = document.getElementsByName("box");
for (var i = 0; i < tBox.length; i++) {
tBox[i].checked = document.getElementById("all").checked;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="checkbox" name="box" value="1" />苹果<br />
<input type="checkbox" name="box" value="2" />三星<br />
<input type="checkbox" name="box" value="3" />诺基亚<br />
<input type="checkbox" name="box" value="4" />索尼<br />
<input type="checkbox" id="all" value="5" onclick="checkAll()" />全选
</div>
</form>
</body>
</html>