<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>javascript checkbox全选 反选 全不选</title> <meta name="description" content=""> <meta name="keywords" content=""> <link href="" rel="stylesheet"> </head> <body> <form action=""> <p><input type="checkbox"></p> <p><input type="checkbox"></p> <p><input type="checkbox"></p> <p><input type="checkbox"></p> <p><input type="checkbox"></p> <p><input type="checkbox"></p> </br> <input type="button" id="a_xuan" value="全选" onclick="all_xuan()"> <input type="button" id="a_n_xuan" value="反选" onclick="fan_xuan()"> <input type="button" id="f_xuan" value="全不选" onclick="all_no_xuan()"> </form> </body> <script> //全选 function all_xuan() { var all_xuan=document.getElementById(‘a_xuan‘); if(all_xuan!=‘‘) { var input_c=document.getElementsByTagName(‘input‘); for(var i=0;i<input_c.length;i++) { input_c[i].checked="true"; } } } //全不选 function all_no_xuan() { var all_xuan=document.getElementById(‘f_xuan‘); if(all_xuan!=‘‘) { var input_c=document.getElementsByTagName(‘input‘); for(var i=0;i<input_c.length;i++) { input_c[i].checked=""; } } } //反选 function fan_xuan() { var all_xuan=document.getElementById(‘a_n_xuan‘); if(all_xuan!=‘‘) { var input_c=document.getElementsByTagName(‘input‘); for(var i=0;i<input_c.length-1;i++) { if(input_c[i].checked) { input_c[i].checked=""; }else { input_c[i].checked="true"; } } } } </script> </html>
时间: 2024-12-06 18:58:40