zoukankan      html  css  js  c++  java
  • 使用JQuery获取被选中的checkbox的value值 以及全选、反选

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

     实现全选反选 注意用:prop

    <ul id = "list-unstyled" class="list-unstyled">
        <li><input name="check" type="checkbox" value="9-DM"></li>
        <li><input name="check" type="checkbox" value="10-DM"></li>
        <li><input name="check" type="checkbox" value="11-DM"></li>
        <li><input name="check" type="checkbox" value="12-DM"></li>
    </ul>
        // 全选
       
        $('#allcheck').click(function(){
    
            $('input[name="check"]').prop('checked','true');
        });
    
        //反选
        $('#reversecheck').click(function(){
            $('input[name="check"]').each(function () {
                $(this).prop("checked", !$(this).prop("checked"));
            });
    
        });
  • 相关阅读:
    Java 写GBK 、utf8格式的文件 java
    NIOnio的美文分享一下,最近喜欢上了Nio希望能给大家扫扫盲
    maven入门和进阶 基础入门 希望帮助大家maven 教程
    log4j 基础
    FastDFS架构剖析(非常值得一看的架构分析和解读)
    FastDFS分布式文件系统问题总汇
    oracle 建表创建外键
    Mybatis下log4j日志输出不正常的解决办法 ,很实用哦 !!!!
    httpclient入门例子 demos
    Firebug http请求响应时间线
  • 原文地址:https://www.cnblogs.com/zhaoyingjie/p/6622743.html
Copyright © 2011-2022 走看看