Javascript全选反选函数

Js函数部分..

//2011-04-28
//by hkshadow
function CheckAll(s,b){
	var a = document.getElementById(s).getElementsByTagName("input");
	var selall=document.getElementById(b).checked;
	for (var i=0; i<a.length; i++)
	   if(selall==true){
       		for (var i=0; i<a.length; i++)
	   				if (a[i].type == "checkbox") a[i].checked = true;
			 }else{ 		
  				for (var i=0; i<a.length; i++)	
       			if (a[i].type == "checkbox") a[i].checked = false ;
			}
	}

HTML演示部分..

                            <ul class="mix2">
               <li class="mix1">解析几何与高等代数</li>
               <li class="mix2"  id="fk1">
              
                              <em>
               <a href="#none" onClick="alert('缺少页面,未作');">1996[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202157">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">1997[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202158">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">1998[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202159">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">1999[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202160">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">2000[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202161">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">2001[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202162">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">2002[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202163">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">2005[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202164">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">2007[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202165">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">2007答案[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202166">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">2008[5元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202167">
               </em>
			               
               <em><span>全选</span>&nbsp;<input type="checkbox" name="all" id="ok1" onclick="CheckAll('fk1','ok1')" class="text"></em>
               
               </li>
              </ul>
                         <ul class="mix2">
               <li class="mix1">数学分析</li>
               <li class="mix2"  id="fk2">
              
                              <em>
               <a href="#none" onClick="alert('缺少页面,未作');">1996[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202149">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">1997[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202150">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">1998[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202151">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">1999[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202152">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">2000[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202153">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">2001[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202154">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">2002[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202155">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">2008[5元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202156">
               </em>
			               
               <em><span>全选</span>&nbsp;<input type="checkbox" name="all" id="ok2" onclick="CheckAll('fk2','ok2')" class="text"></em>
               
               </li>
              </ul>
                         <ul class="mix2">
               <li class="mix1">数学学院专业综合考试</li>
               <li class="mix2"  id="fk3">
              
                              <em>
               <a href="#none" onClick="alert('缺少页面,未作');">1996[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202168">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">1997[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202169">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">1998[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202170">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">1999[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202171">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">2000[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202172">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">2001[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202173">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">2002[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202174">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">2004[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202175">
               </em>
			               <em>
               <a href="#none" onClick="alert('缺少页面,未作');">2005[3元]</a>&nbsp;
               <input type="checkbox" name="checks[]" class="text" value="202176">
               </em>
			               
               <em><span>全选</span>&nbsp;<input type="checkbox" name="all" id="ok3" onclick="CheckAll('fk3','ok3')" class="text"></em>
               
               </li>
              </ul>

Leave a Reply

(will not be published)