使用jQuery獲得選中複選框個數

下面介紹兩種使用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>


 

發佈了59 篇原創文章 · 獲贊 2 · 訪問量 36萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章