checkbox+jquery 前台获取checkbox的val值

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

      <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>
发布了251 篇原创文章 · 获赞 42 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_38992403/article/details/103985577