摘要:<!DOCTYPE html> <html> <head> <title>全选</title> <style type="text/css"> .box { width: 120px; height: 300px; border:1
<!DOCTYPE html> <html> <head> <title>全选</title> <style type="text/css"> .box { width: 120px; height: 300px; border:1px solid #ccc; border-radius: 5px; padding: 5px 10px; margin: 20px auto; } .box div { border-bottom: 1px solid #ccc; padding-bottom: 10px; } .box input { margin: 8px; } button { float: right; background-color: #F40; border: none; color: #fff; width: 60px; height: 30px; } </style> <script type="text/javascript"> function checkall(){ var checkall,item; checkall = document.getElementById('checkall');//获取全选框 item = document.getElementsByName('item[]')//获取下面的勾选框 for(var i =0;i<item.length;i++){ if(checkall.checked){ item[i].checked = true; }else { item[i].checked = false; } } } </script> </head> <body> <div class="box"> <div> <input type="checkbox" id="checkall" onclick = "checkall()"><label for="checkall">全选</label> </div> <input type="checkbox" name="item[]">红旗<br> <input type="checkbox" name="item[]">比亚迪<br> <input type="checkbox" name="item[]">北京现代<br> <input type="checkbox" name="item[]">雷克萨斯<br> <input type="checkbox" name="item[]">法拉利<br> <input type="checkbox" name="item[]">劳斯莱斯<br> <input type="checkbox" name="item[]">保时捷<br> <input type="checkbox" name="item[]">玛莎拉蒂<br> <button>结算</button> </div> </body> </html>
批改老师:天蓬老师批改时间:2018-12-29 09:25:35
老师总结:全选效果实现的不太理想,checkbox在点击的时候通过this可以拿到当前的checkbox对象,不需要再用id选择器再选一遍