jQuery 计算checkbox被选中的数量

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">
		<title>My JSP 'Jquery004.jsp' starting page</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		
		<script type="text/javascript" src="js/jquery-1.6.1.js"></script>

		<!-- 用javascript算出checkbox被选中的数量
		<script type="text/javascript">
			window.onload = function(){
				var button1=document.getElementById("button1");
				button1.onclick = function(){
					var checkboxs = document.getElementsByName("myCheck");
					var num=0;
					for(var i=0;i<checkboxs.length;i++){
						if(checkboxs[i].checked){
							num++;
					 	}
					}
				 	alert(num);
				}
			}
		</script>
		-->
		<!-- 用jQuery算出checkbox被选中的数量 -->
		<script type="text/javascript">
			$(document).ready(function() {
				$("#button1").click(function() {
					alert($("input[type=checkbox]:checked").length);
				});
			});
		</script>
	</head>

	<body>
		<input type="checkbox" name="myCheck" checked="checked" />
		<input type="checkbox" name="myCheck" />
		<input type="checkbox" name="myCheck" />
		<input type="checkbox" name="myCheck" checked="checked" />
		<input type="checkbox" name="myCheck" />
		<input type="checkbox" name="myCheck" />
		<br>
		<br>
		<input id="button1" type="button" value="按钮" />
	</body>
</html>
 

你可能感兴趣的:(JavaScript,html,jquery,jsp,cache)