实现点击按钮全选功能

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
 5 <title>无标题文档</title>
 6 <script language="javascript"> 
 7 function selectAll(name) 
 8 { 
 9 var el=document.getElementsByName("coffee"); 
10 for (var i=0;i<el.length;i++) 
11 { 
12 if(el[i].type=="checkbox"&&el[i].name==name) 
13 { 
14 el[i].checked=true; 
15 
16 } 
17 } 
18 } 
19 
20 function clearAll(name) 
21 { 
22 var el=document.getElementsByName("coffee"); 
23 for (var i=0;i<el.length;i++) 
24 { 
25 if(el[i].type=="checkbox"&&el[i].name==name) 
26 { 
27 el[i].checked=false; 
28 } 
29 } 
30 }
31 
32 </script> 
33 </head>
34 
35 <body>
36 <p>以下是一个调查结果</p> 
37 <form> 
38 <input type="checkbox" name="coffee" value="apple">apple 
39 <input type="checkbox" name="coffee" value="pear">pear 
40 <input type="checkbox" name="coffee" value="berry">berry 
41 <p><p> 
42 <input type="button" value="全选" onclick="selectAll('coffee')"> 
43 <input type="button" value="取消全选" onclick="clearAll('coffee')"> 
44 </form> 
45 </body>
46 </html>

 

posted @ 2016-05-03 14:06  LeBron_J  阅读(623)  评论(0编辑  收藏  举报