獲取 checkbox 的選中個數(轉)


獲取 checkbox的 選中個數可以直接使用如下jquery語法:

1 $("input[type='checkbox']:checked").length;

示例如下:

  1. 創建Html代碼及css樣式 

    <div> 
        <input type="checkbox" name="fruit"> apple
        <input type="checkbox" name="fruit"> orange
        <input type="checkbox" name="fruit"> banana
        <input type="checkbox" name="fruit"> watermelon
        <input type="button" value="I like these fruit!">
    </div>
    div{width:500px;padding:20px;border:4px solid #ebcbbe;}
    input{margin:10px 5px;}

     

  2. jquery代碼

    $(function(){
        $("input[type='button']").click(function() {
            alert($("input[type='checkbox']:checked").length);
        });
    })

     

  3. 效果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM