获取CHECKBOX

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yelin042/article/details/88660411

<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">

<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());
        });
    });


    $("#b2").click(function() {
        var html = "";
        $.each($('input:checkbox'), function() {
            if (this.checked) {
                html += $(this).val() + ",";
            }
        });

        window.alert("你选了:" +
                        $('input[type=checkbox]:checked').length + "个,其中有:" + html);
    });
    </script>

猜你喜欢

转载自blog.csdn.net/yelin042/article/details/88660411
今日推荐