js 单选框 全选、全不选,反选 的操作
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>复选框</title>
<style type="text/css">
</style>
</head>
<body>
<input type="checkbox" name="love" />篮球
<br />
<input type="checkbox" name="love" />排球
<br />
<input type="checkbox" name="love" />羽毛球
<br />
<input type="checkbox" name="love" />乒乓球
<br />
<input type="button" value="全选" onclick="setAll()" />
<input type="button" value="全不选" onclick="setNo()" />
<input type="button" value="反选" onclick="setOthers()" />
<script type="text/javascript">
function setAll() {
var loves = document.getElementsByName("love");
for(var i = 0; i < loves.length; i++) {
loves[i].checked = true;
}
}
function setNo() {
var loves = document.getElementsByName("love");
for(var i = 0; i < loves.length; i++) {
loves[i].checked = false;
}
}
function setOthers() {
var loves = document.getElementsByName("love");
for(var i = 0; i < loves.length; i++) {
if(loves[i].checked == false)
loves[i].checked = true;
else
loves[i].checked = false;
}
}
</script>
</body>
</html>
</body>
</html>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b330d54e027cfa89910ed46facc813d6.png#pic_center)