javaEE (四)javaweb_JavaScript (5) 表單全選
阿新 • • 發佈:2018-12-22
Date:2018/11/17
效果如圖:點選一個,其他全選,可以復原:
原始碼:
<!-- 1、確定事件:onclick 繫結到複選框裡面; -->
<!-- 2、獲取複選框:var checkAllEle = document.getElementById("id") -->
<!-- 3、複選框狀態:checkAllEle.checked -->
<!-- 4、獲取下面所有複選框:document.getElementsByName("name") -->
<!-- 注: document.getElementById('checkAll' )返回的是一個集合(js裡面只有陣列,所以要用遍歷下標來運算元組) -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<script>
function checkAll(){
var check = document.getElementsByName('checkOne');
if(document.getElementById('checkAll' ).checked == true){
for(var i = 0; i < check.length; i++){
check[i].checked = true;
}
}else{
for(var i = 0; i < check.length; i++){
check[i].checked = false;
}
}
}
</script>
<body>
<table border="1" width="500" height="50" align="center">
<thead>
<tr>
<td colspan="4">
<input type="button" value="新增" />
<input type="button" value="刪除" />
</td>
</tr>
<tr>
<th><input type="checkbox" onclick="checkAll()" id="checkAll" /></th>
<th>編號</th>
<th>姓名</th>
<th>年齡</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" name="checkOne" /></td>
<td>1</td>
<td>張三</td>
<td>22</td>
</tr>
<tr>
<td><input type="checkbox" name="checkOne" /></td>
<td>2</td>
<td>李四</td>
<td>25</td>
</tr>
<tr>
<td><input type="checkbox" name="checkOne" /></td>
<td>3</td>
<td>王五</td>
<td>27</td>
</tr>
<tr>
<td><input type="checkbox" name="checkOne" /></td>
<td>4</td>
<td>趙六</td>
<td>29</td>
</tr>
<tr>
<td><input type="checkbox" name="checkOne" /></td>
<td>5</td>
<td>田七</td>
<td>30</td>
</tr>
<tr>
<td><input type="checkbox" name="checkOne" /></td>
<td>6</td>
<td>汾九</td>
<td>20</td>
</tr>
</tbody>
</table>
</body>
</html>