日期:2014-05-16 浏览次数:20455 次
<input id="all" type="checkbox" value="全选" onclick="choiceall()" />
<input name="product" type="checkbox" value="1" onclick="judgethechoice()" /> <input name="product" type="checkbox" value="2" onclick="judgethechoice()" /> <input name="product" type="checkbox" value="3" onclick="judgethechoice()" /> <input name="product" type="checkbox" value="4" onclick="judgethechoice()" />
function choiceall() { var getall=document.getElementById("all"); var getproduct=document.getElementsByName("product"); for(i=0;i<getproduct.length;i++) { if(getall.checked==true) getproduct[i].checked=true; else getproduct[i].checked=false; } } function judgethechoice() { var getall=document.getElementById("all"); var getproduct=document.getElementsByName("product"); for(i=0;i<getproduct.length;i++) { if(getproduct[i].checked==false) getall.checked=false; if(getproduct[i].checked==true&&getall.checked==false) getall.checked=true; } }
<!DOCTYPE HTML> <html> <head> <meta charset="gb2312" /> <title></title> <style> </style> </head> <body> 全选<input id="all" type="checkbox" value="全选" /> <br /> <input name="product" type="checkbox" value="1" /> <input name="product" type="checkbox" value="2" /> <input name="product" type="checkbox" value="3" /> <input name="product" type="checkbox" value="4" /> <script> ;(function(){ var all = document.getElementById('all'); var objs = document.getElementsByName('product'); var len = objs.length; document.onclick = function(e){ e = window.event || e; var t = e.srcElement || e.target; var name = t.tagName.toLowerCase(); if( name == 'input' && t.type == 'checkbox' ){ if( t.id == 'all' ){ var flag = t.checked; for(var i = 0; i < len; i++){ if( flag ){ objs[i].checked = true; }else{ objs[i].checked = false; } } }else if( t.name == 'product' ){