<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<label>请选择你熟悉的编程语言</label>
<input type="checkbox" name="lang" value="Java"/>Java
<input type="checkbox" name="lang" value="JavaScript"/>JavaScript
<input type="checkbox" name="lang" value="C++"/>C++
<input type="checkbox" name="lang" value="T-SQL"/>T-SQL
<input type="checkbox" name="lang" value="PHP"/>PHP
<br>
<input type="checkbox" οnclick="checkAll(this.checked,0)"/>全选
<input type="checkbox" οnclick="checkAll(this.checked,1)"/>反选
<input type="checkbox" οnclick="checkAll(this.checked,2)"/>不选
<script>
function checkAll(flag,f){
//获取所有复选框对象
var cks = document.getElementsByName('lang');
for(var i = 0;i<cks.length;i++)
{
switch(f){
case 0:
//全选
cks[i].checked=flag;
break;
case 1:
//反选
//获取当前遍历到的复选框状态
cks[i].checked = cks[i].checked == true ? false:true;
break;
case 2:
//不选
cks[i].checked = false;
break;
}
}
}
</script>
</body>
</html>
javascript之全选、反选和不选
最新推荐文章于 2020-12-05 14:02:03 发布