获取checkbox复选框的值

时间:2022-03-20 03:44:01
<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>