• jQuery--checkbox全选/取消全选


    用JavaScript使页面上的一组checkbox全选/取消全选,逻辑很简单,实现代码也没有太难的语法。但使用jQuery实现则更简单,代码也很简洁,精辟!

    jQuery版本:1.3.2

    <html>
    <head>
    <script src="jquery-1.3.2.min.js" type="text/javascript"></script>
    </head>
    <body>
    <input type="checkbox" name="chk_list" id="chk_list_1" value="1" />1<br />
    <input type="checkbox" name="chk_list" id="chk_list_2" value="2" />2<br />
    <input type="checkbox" name="chk_list" id="chk_list_3" value="3" />3<br />
    <input type="checkbox" name="chk_list" id="chk_list_4" value="4" />4<br />
    <input type="checkbox" name="chk_all" id="chk_all" />全选/取消全选
    <script type="text/javascript">
    $("#chk_all").click(function(){
         $("input[name='chk_list']").attr("checked",$(this).attr("checked"));
    });
    </script>
    </body>
    </html>
    

    jQuery.attr  获取/设置对象的属性值,如:

    $("input[name='chk_list']").attr("checked");     //读取所有name为'chk_list'对象的状态(是否选中)

    $("input[name='chk_list']").attr("checked",true);      //设置所有name为'chk_list'对象的checked为true

    再如:

    $("#img_1").attr("src","test.jpg");    //设置ID为img_1的<img>src的值为'test.jpg'
    $("#img_1").attr("src");     //读取ID为img_1的<img>src值

     下面的代码是获取上面实例中选中的checkbox的value值:

    <script type="text/javascript">
        //获取到所有name为'chk_list'并选中的checkbox(集合)
        var arrChk=$("input[name='chk_list]:checked");
        //遍历得到每个checkbox的value值
        for (var i=0;i<arrChk.length;i++)
        {
             alert(arrChk[i].value);
        }
    </script>
    

     循环遍历每一个元素的值:

    <script type="text/javascript">
        var arrChk=$("input[name='chk_list']:checked");
        $(arrChk).each(function(){
           window.alert(this.value);                        
        }); 
    });
    </script>
    
  • 相关阅读:
    Learning to Segment Every Thing(偏监督学习)
    特征金字塔-Feature Pyramid Networks for Object Detection
    Mask R-CNN翻译
    04.卷积神经网络_第一周卷积神经网络
    对极几何
    hdu 3572 Task Schedule【 最大流 】
    uva 11624 Fire! 【 BFS 】
    hdu 3549 Flow Problem 【最大流】
    codeforces 277 A Learning Languages 【DFS 】
    poj 2828 Buy Tickets【线段树 单点更新】
  • 原文地址:https://www.cnblogs.com/wangzhuxing/p/5316545.html
Copyright © 2020-2023  润新知