16_JavaScript_DOM编程_实验之Checkbox全选 Posted on 2019-07-20 In javascript Views: 本文主要学习如何对复选框进行操作…… 一个练习: 关于对复选框的操作: ex-7.html: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>复选框操作</title><script type="text/javascript"> window.onload = function() { //获取四个复选框的节点 var itemsNode = document.getElementsByName("items"); //alert(itemsNode.length);//返回4 //获取全选/全不选的节点 var checkedAll_2 = document.getElementById("checkedAll_2"); //alert(checkedAll_2);//返回元素节点 //alert(checkedAll_2.checked);//false //进入之后首先判断全选和复选框是否全部对应 //checkedAll_2.checked = true;//测试 if(checkedAll_2.checked){ allChecked(itemsNode, checkedAll_2); }else{ allNoChecked(itemsNode, checkedAll_2); } //当改变checkedAll_2时改变复选框的值 checkedAll_2.onchange = function(){ //alert("改变了"); if(checkedAll_2.checked){ allChecked(itemsNode, checkedAll_2); }else{ allNoChecked(itemsNode, checkedAll_2); } } //当改变某一个复选框的值时判断整体是否符合全选或者全不选条件 for(var i=0; i<itemsNode.length; i++){ itemsNode[i].onchange = function(){ if(this.checked){//如果选中了当前项,判断是否全选 //如果四个中有一个没有被选中截止添加t的值 for(var t=0; t<itemsNode.length; t++){ if(!itemsNode[t].checked){ break; } } //如果t值累计到4说明全部选取,将全选打勾 if(t == itemsNode.length){ checkedAll_2.checked = true; } }else{//否者说明该项不打勾,此时判断是否没有全选 //判断全选如果有打勾需要去掉 if(checkedAll_2.checked){ checkedAll_2.checked = false; } } } } //接下来为各个按键添加事件 //全选按钮 var allCheckedButton = document.getElementById("CheckedAll"); allCheckedButton.onclick = function(){ allChecked(itemsNode, checkedAll_2); } //全不选按钮 var allNoCheckedButton = document.getElementById("CheckedNo"); allNoCheckedButton.onclick = function(){ allNoChecked(itemsNode, checkedAll_2); } //反选按钮: var otherCheckedButton = document.getElementById("CheckedRev"); otherCheckedButton.onclick = function(){ for(var i=0; i<itemsNode.length; i++){ if(itemsNode[i].checked){ itemsNode[i].checked = false; }else{ itemsNode[i].checked = true; } //添加一个全选或者不全选的操作 operateAllChecked(itemsNode, checkedAll_2); } } //测试读出复选框的值 //alert(itemsNode[0].value); //提交按钮: var submitButton = document.getElementById("send"); submitButton.onclick = function(){ //设置变量text用来载值 var text = ""; //遍历出选中的值 for(var i=0; i<itemsNode.length; i++){ if(itemsNode[i].checked){ if(text==""){ text = itemsNode[i].value; }else{ text = text + "+" + itemsNode[i].value; } } } alert(text); } //判断程序是否执行完 alert("end"); } function operateAllChecked(itemsNode, checkedAll_2){ for(var t=0; t<itemsNode.length; t++){ if(!itemsNode[t].checked){ break; } } //如果t值累计到4说明全部选取,将全选打勾 if(t == itemsNode.length){ checkedAll_2.checked = true; }else{ checkedAll_2.checked = false; } } function allChecked(itemsNode, checkedAll_2){ for(var i=0; i<itemsNode.length; i++){ itemsNode[i].checked = true; } checkedAll_2.checked = true; } function allNoChecked(itemsNode, checkedAll_2){ for(var i=0; i<itemsNode.length; i++){ itemsNode[i].checked = false; } checkedAll_2.checked = false; }</script></head><body> <form method="post" action=""> 你爱好的运动是?<input type="checkbox" id="checkedAll_2" />全选/全不选 <br /> <input type="checkbox" name="items" value="足球" />足球 <input type="checkbox" name="items" value="篮球" />篮球 <input type="checkbox" name="items" value="羽毛球" />羽毛球 <input type="checkbox" name="items" value="乒乓球" />乒乓球 <br /> <input type="button" id="CheckedAll" value="全 选" /> <input type="button" id="CheckedNo" value="全不选" /> <input type="button" id="CheckedRev" value="反 选" /> <input type="button" id="send" value="提 交" /> </form></body></html>