热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

js复选框的全选和取消全选的实现

js实现:

js实现:

DOCTYPE html>
<html lang&#61;"en">
<head><meta charset&#61;"UTF-8"><meta http-equiv&#61;"X-UA-Compatible" content&#61;"IE&#61;edge"><meta name&#61;"viewport" content&#61;"width&#61;device-width, initial-scale&#61;1.0"><title>Documenttitle>
head>
<body><script type&#61;"text/Javascript">window.onload &#61; function(){var quan &#61; document.getElementById("quan");quan.onclick &#61; function(){var aihaos &#61; document.getElementsByName("aihao");for(var i &#61; 0; i<aihaos.length ; i&#43;&#43;){if(quan.checked){aihaos[i].checked &#61; true;}else{aihaos[i].checked &#61; false;}}}}script><input type&#61;"checkbox" id&#61;"quan"/><br><input type&#61;"checkbox" name&#61;"aihao" value&#61;"smoke"/>抽烟<br><input type&#61;"checkbox" name&#61;"aihao" value&#61;"drink"/>喝酒<br><input type&#61;"checkbox" name&#61;"aihao" value&#61;"firehair"/>烫头<br>
body>
html>

简化版&#xff1a;

DOCTYPE html>
<html lang&#61;"en">
<head><meta charset&#61;"UTF-8"><meta http-equiv&#61;"X-UA-Compatible" content&#61;"IE&#61;edge"><meta name&#61;"viewport" content&#61;"width&#61;device-width, initial-scale&#61;1.0"><title>Documenttitle>
head>
<body><script type&#61;"text/Javascript">window.onload &#61; function(){var quan &#61; document.getElementById("quan");quan.onclick &#61; function(){var aihaos &#61; document.getElementsByName("aihao");for(var i &#61; 0; i<aihaos.length ; i&#43;&#43;){aihaos[i].checked &#61; quan.checked;}}}script><input type&#61;"checkbox" id&#61;"quan"/><br><input type&#61;"checkbox" name&#61;"aihao" value&#61;"smoke"/>抽烟<br><input type&#61;"checkbox" name&#61;"aihao" value&#61;"drink"/>喝酒<br><input type&#61;"checkbox" name&#61;"aihao" value&#61;"firehair"/>烫头<br>
body>
html>

上面两种都有错误&#xff0c;全部选中后&#xff0c;取消下面任意一个&#xff0c;上面的全选还会打钩&#xff0c;下面是最终的完善版本&#xff1a;

DOCTYPE html>
<html lang&#61;"en">
<head><meta charset&#61;"UTF-8"><meta http-equiv&#61;"X-UA-Compatible" content&#61;"IE&#61;edge"><meta name&#61;"viewport" content&#61;"width&#61;device-width, initial-scale&#61;1.0"><title>Documenttitle>
head>
<body><script type&#61;"text/Javascript">window.onload &#61; function(){var quan &#61; document.getElementById("quan");var aihaos &#61; document.getElementsByName("aihao");quan.onclick &#61; function(){for(var i &#61; 0; i<aihaos.length ; i&#43;&#43;){aihaos[i].checked &#61; quan.checked;}}for(var i &#61; 0; i<aihaos.length ; i&#43;&#43;){aihaos[i].onclick &#61; function(){var count &#61; aihaos.length;var checkedCount &#61; 0;for(var i &#61; 0; i<aihaos.length; i&#43;&#43;){if(aihaos[i].checked){checkedCount&#43;&#43;;}}quan.checked &#61; (count &#61;&#61;checkedCount);}}}script><input type&#61;"checkbox" id&#61;"quan"/><br><input type&#61;"checkbox" name&#61;"aihao" value&#61;"smoke"/>抽烟<br><input type&#61;"checkbox" name&#61;"aihao" value&#61;"drink"/>喝酒<br><input type&#61;"checkbox" name&#61;"aihao" value&#61;"firehair"/>烫头<br>
body>
html>


推荐阅读
author-avatar
AT
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有