全选操作和取消全选操作

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function selectAll(obj){
var obj = document.getElementsByName("checkbox");
if(null!=obj){
alert("全选操作!!!");
for (var i = 0; i < obj.length; i++) {
obj[i].checked = "checked";
}
}
}
function unselectAll(obj){
var obj = document.getElementsByName("checkbox");
if(null!=obj){
alert("取消全选操作!!!");
for (var i = 0; i < obj.length; i++) {
obj[i].checked = "";
}
}
}
</script>
</head>
<body>
<p>全选和不全选的问题</p>
<table border="1" style="position: center;">
<tr>
<td>读书</td>
<td><input type="checkbox" name="checkbox" value="1"/></td>
</tr>
<tr>
<td>写字</td>
<td><input type="checkbox" name="checkbox" value="2"/></td>
</tr>
<tr>
<td>运动</td>
<td><input type="checkbox" name="checkbox" value="3"/></td>
</tr>
<tr>
<td colspan="2">
&nbsp;
<button type="button" onclick="selectAll('checkbox')">全选操作</button>
&nbsp;
<button type="button" onclick="unselectAll('checkbox')">取消全选</button>
</td>

</tr>
</table>
</body>
</html>

posted @ 2017-09-19 17:16  caocx  阅读(643)  评论(0编辑  收藏  举报