获取checkbox复选框的值

<title>CheckBox</title> <script type = "text/javascript"> doGetVal: function doGetVal() { //var fruitName = document.getElementById("apple").value;//根据ID获取值 //alert(fruitName); // var fruit = document.getElementsByName("fruit"); //根据Name拿到集合数组 for (var i=0;i<fruit.length;i++ ) { if (fruit[i].checked)//checkbox是否选中 { var fruitName = fruit[i].value; alert(fruitName); } } } </script> </head> <body> <div align="left"> <input type = "checkBox" id="apple" name="fruit" value="apple"/>apple<br> <input type = "checkBox" name="fruit" value="watermelon"/>watermelon<br> <input type = "checkBox" name="fruit" value="strawberry"/>strawberry<br> <input type = "button" onclick="doGetVal()" value = "click"/> </div> </body> </html>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgwypf.html