javascript的数组方法push说明
1 <!doctype html> 2 <meta charset="utf-8"> 3 <body> 4 <input type="checkbox" value="1" name="check" checked="checked"/> 5 <input type="checkbox" value="1" name="check"/> 6 <input type="checkbox" value="1" name="check" checked="checked"/> 7 <input type="checkbox" value="1" name="check" /> 8 <input type="button" value="你选中的个数" id="btn" /> 9 <script> 10 11 var btn=document.getElementById('btn'); 12 btn.onclick=function(){ 13 var arrays=new Array(); 14 var checkitem=document.getElementsByName("check"); 15 for(var i=0;i<checkitem.length;i++) 16 { 17 if(checkitem[i].checked){ 18 arrays.push(checkitem[i].value);//将()里面的值传入arrays数组中 19 } 20 } 21 alert(arrays.length) 22 } 23 24 </script> 25 </body>
if you don't try,you will never know!