惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
有赞技术团队
有赞技术团队
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
D
DataBreaches.Net
F
Full Disclosure
腾讯CDC
博客园 - 【当耐特】
MyScale Blog
MyScale Blog
Stack Overflow Blog
Stack Overflow Blog
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
The Register - Security
The Register - Security
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
J
Java Code Geeks
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy International News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
A
Arctic Wolf
Scott Helme
Scott Helme
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tor Project blog
博客园 - 三生石上(FineUI控件)
Know Your Adversary
Know Your Adversary
AWS News Blog
AWS News Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
CERT Recently Published Vulnerability Notes
O
OpenAI News
Project Zero
Project Zero
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Application and Cybersecurity Blog
Application and Cybersecurity Blog
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
MongoDB | Blog
MongoDB | Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
Schneier on Security
Schneier on Security

博客园 - 码农张3

Vue3 父组件引用子组件并控制子组件显隐及数据传递 MySQL创建用户且只能访问指定数据库表 自定义跨字段校验必填注解 Element Plus SCSS 变量覆盖用法 leaflet-canvasmarker添加的marker旋转问题 SpringBoot项目控制台打印sql设置 Windows 远程桌面复制粘贴突然无效 SQLServerException: 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。 右键用vscode打开文件夹 nodemon: 无法加载文件 C:\DevSoft\nodejs\node_global\nodemon.ps1 - 码农张3 EasyUI Datagrid添加右键导出菜单并导出数据 个人所得税App住房贷款利息解读 JavaScript学习笔记—DOM:操作class JavaScript学习笔记—DOM:通过属性读取样式 SQLServer数据库导出指定表里所有数据成insert语句 解决Mybatis xml文件中执行mysql语句时,语句后携带分号实现多语句执行报异常问题 JS去除字符串前面所有0 Redis启动服务报错:服务没有及时响应启动或者控制请求 JavaScript学习笔记—DOM:元素的添加、修改、删除 JavaScript学习笔记—DOM:事件 JavaScript学习笔记—DOM:属性节点
JavaScript学习笔记—全选、取消、反选、提交
码农张3 · 2023-02-02 · via 博客园 - 码农张3
<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>JS全选、取消、反选、提交</title>
    <script>
	window.onload = function(){
	  // 获取全选按钮
	  const allBtn = document.getElementById("all");
	  // 获取全选复选框
	  const checkAllBox = document.getElementById("check-all");
	  // 获取取消按钮
	  const noBtn = document.getElementById("no");
	  // 获取反选按钮
	  const reverseBtn = document.getElementById("reverse");
	  // 获取4个爱好复选框
	  const hobbies = document.getElementsByName("hobby");
	  // 为全选按钮绑定单击响应函数
	  allBtn.onclick = function(){
	    // 将4个复选框设置为选中状态
	    for(let i = 0; i < hobbies.length; i++){
	      hobbies[i].checked = true;
	    }
	    // 将全选复选框设置为选中状态
	    checkAllBox.checked = true;
	  };
	  // 为取消按钮绑定单击相应函数
	  noBtn.onclick = function(){
	    // 将4个复选框设置为取消状态
	    for(let i = 0; i < hobbies.length; i++){
	      hobbies[i].checked = false;
	    }
	    // 将全选复选框设置为取消状态
	    checkAllBox.checked = false;
	  };
	  // 为反选按钮绑定单击相应函数
	  reverseBtn.onclick = function(){
	    for(let i = 0; i < hobbies.length; i++){
	      hobbies[i].checked = !hobbies[i].checked;
	    }
	    // 获取所有选中的checkbox
	    const checkedBox = document.querySelectorAll("[name=hobby]:checked");
	    // 判断hobbies是否全选,如果全选了 全选复选框 设置为选中状态,否则设置为取消状态
	    if (hobbies.length === checkedBox.length) {
	      checkAllBox.checked = true;
	    } else {
	      checkAllBox.checked = false;
	    }
	  };
	  // 为全选复选框绑定单击相应函数
	  checkAllBox.onclick = function(){
	    for(let i = 0; i < hobbies.length; i++){
	      // 在事件的响应函数中,响应函数绑定给谁 this就是谁(箭头函数除外)
	      hobbies[i].checked = this.checked;
	    }
	  };
	  // 使全选checkbox和四个爱好checkbox同步
	  for(let i = 0; i < hobbies.length; i++){
	    hobbies[i].onclick = function(){
	      // 获取所有爱好复选框 选中的checkbox
	      const checkedBoxs = document.querySelectorAll("[name=hobby]:checked");
	      // 判断hobbies是否全被选中
	      if(hobbies.length === checkedBoxs.length){
	        checkAllBox.checked = true;
	      }else{
	        checkAllBox.checked = false;
	      }
	    };
	  }
	  // 提交按钮
	  const sendBtn = document.getElementById("send");
	  sendBtn.onclick = function(){
	    for (let i = 0; i < hobbies.length; i++) {
	      hobbies[i].checked && alert(hobbies[i].value);
	    }
	  };
	};
    </script>
  </head>
  <body>
    <div>
      <form action="#">
        <div>
          请选择你的爱好:
          <input type="checkbox" id="check-all" /> 全选
        </div>
        <div>
          <input type="checkbox" name="hobby" value="乒乓球" /> 乒乓球
          <input type="checkbox" name="hobby" value="篮球" /> 篮球
          <input type="checkbox" name="hobby" value="羽毛球" /> 羽毛球
          <input type="checkbox" name="hobby" value="足球" /> 足球
        </div>
        <div>
          <button type="button" id="all">全选</button>
          <button type="button" id="no">取消</button>
          <button type="button" id="reverse">反选</button>
          <button type="button" id="send">提交</button>
        </div>
      </form>
    </div>
  </body>
</html>