javascriptチェックボックスの選択個数を計算します.

1389 ワード


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<input type="checkbox" value="1" name="check" checked="checked"/>
<input type="checkbox" value="2" name="check"/>
<input type="checkbox" value="3" name="check" checked="checked"/>
<input type="button" value="      " id="btn">

<script type="text/javascript">
	var btn = document.getElementById("btn");
	btn.onclick = function(){
		var arrays = new Array();
		var items = document.getElementsByName("check");
		for(i=0; i<items.length; i++){
			if(items[i].checked){
				arrays.push(items[i].value);
			}
		}
		alert("      :" + arrays.length);
	}
</script>
</body>
</html>