在操作一些后台的时候,用到全选,反选,全不选这些操作,那么js代码怎么实现呢
<div> <input type="checkbox" name="cbo"/><br/> <input type="checkbox" name="cbo"/><br/> <input type="checkbox" name="cbo"/><br/> </div> <input type="button" value="全选" onclick="CheckAll()"/> <input type="button" value="不选" onclick="UnCheck()"/> <input type="button" value="反选" onclick="othercheck()"/> <script> var CheckBox = document.getElementsByName("cbo"); //获取name=cbo的控件集合 //全选 function CheckAll(){ for(i=0;i<CheckBox.length;i++){CheckBox[i].checked=true;}; }; //全不选 function UnCheck(){ for(i=0;i<CheckBox.length;i++){CheckBox[i].checked=false;}; }; //反选 function othercheck(){ for(i=0;i<CheckBox.length;i++){ if(CheckBox[i].checked==true){ CheckBox[i].checked=false;} else{ CheckBox[i].checked=true} } }; </script>
然后程序根据接收到的id在做相应的处理,删除,修改等操作。熟悉下js代码也是有好处的