首页 > Web开发 > 详细

使用JQuery获取被选中的checkbox的value值

时间:2021-02-10 12:54:11      阅读:28      评论:0      收藏:0      [点我收藏+]

以下为使用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>

使用JQuery获取被选中的checkbox的value值

原文:https://www.cnblogs.com/sunny3158/p/14395098.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!