下面介紹兩種使用jQuery來獲取頁面中選中複選框個數的方法:
<html>
<head>
<title>test</title>
<script src="jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
//方法一
$(function(){
$("#GetButton_1").click(
function(){
var CheckCount=0;
$("[name='ChooseOne']").each(function(){
if($(this).attr("checked")){
CheckCount++;
}
});
alert(CheckCount);
});
});
//方法二
$(function(){
$("#GetButton_2").click(
function(){
alert($("input[name='ChooseOne']:checked").length);
});
});
</script>
</head>
<body>
<input type="checkbox" name="ChooseOne" >
<input type="checkbox" name="ChooseOne" >
<input type="checkbox" name="ChooseOne" >
<input type="checkbox" name="ChooseOne" >
<input type="checkbox" name="ChooseOne" >
<br>
<input name="GetButton_1" id="GetButton_1" type="button" value="方法一">
<input name="GetButton_2" id="GetButton_2" type="button" value="方法二">
</body>
</html>