Renyi-Fan

jquery如何获取checkbox的值

一、总结

一句话总结:就是通过jquery获取哪些对应name的checkbox,然后找出:check(被选中的),然后通过jquery的each遍历获取这些的值即可

$(\'input[name=\'checkboxName\']:checked\')+.each遍历取值

 

 

 

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

参考:使用JQuery获取被选中的checkbox的value值
https://www.cnblogs.com/td960505/p/6123510.html

以下为使用JQuery获取input checkbox被选中的值代码:

<html>
    <head>
        <meta charset="gbk">
        <!-- 引入JQuery -->
        <script src="jquery-1.3.1.js" type="text/javascript"></script>
    </head>

    <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>
</html>

 

分类:

技术点:

相关文章:

  • 2021-12-08
  • 2021-10-02
  • 2021-10-02
  • 2021-10-02
  • 2022-01-02
  • 2022-01-02
猜你喜欢
  • 2021-10-02
  • 2021-10-02
  • 2021-10-02
  • 2022-01-02
  • 2021-10-02
  • 2021-10-02
  • 2021-11-07
相关资源
相似解决方案