使用JQuery獲取被選中的checkbox的value值


以下為使用JQuery獲取input checkbox被選中的值代碼:

<body>
    <input type="checkbox" value="橘子" name="check">橘子1</input>
    <input type="checkbox" value="香蕉" name="check">香蕉1</input>
    <input type="checkbox" value="西瓜" name="check">西瓜1</input>
    <input type="checkbox" value="芒果" name="check">芒果1</input>
    <input type="checkbox" value="葡萄" name="check">葡萄1</input>
    
    <input type="button" value="方法1" id="b1">
    <input type="button" value="方法2" id="b2">


</body>

<script>
    //方法1
    $("#b1").click(function(){
        //$('input:checkbox:checked') 等同於 $('input[type=checkbox]:checked')
        //意思是選擇被選中的checkbox
        $.each($('input:checkbox:checked'),function(){
            window.alert("你選了:"+
                $('input[type=checkbox]:checked').length+"個,其中有:"+$(this).val());
        });
    });
    
    //方法2
    $("#b2").click(function(){
        $.each($('input:checkbox'),function(){
            if(this.checked){
                window.alert("你選了:"+
                    $('input[type=checkbox]:checked').length+"個,其中有:"+$(this).val());
            }
        });
    });
</script>


免責聲明!

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



猜您在找 使用JQuery獲取被選中的checkbox的value值 使用JQuery獲取被選中的checkbox的value值 使用JQuery獲取被選中的checkbox的value值 以及全選、反選 獲取