欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

获取checkbox复选框的值

程序员文章站 2022-04-05 15:16:52
...
<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>

 

相关标签: checkbox