• JS对checkbox全选和取消全选


    需求:checkbox控制列表数据全选与取消全选择。

    效果图:

    1、html

    <body >
              <input type="button" name="inputfile" id="inputfile" value="点击导入" onclick="open();"/>
              <input type="file" id="File1" name="File1" style="display:none;">
            <input type="button" name="outbtn" value="导出"/>
            
        <table border="1">
            <!-- <tr>
        <a href="javascript:;" class="a-upload"> </a>
                </tr>-->
            <tr> 
                <td><input  id="all" type="checkbox" name="yon" onclick="chk()"/></td>
                <td>ID</td>
                <td>地区</td>
            </tr>
            <c:forEach items="${dislist }" var="dis">
                <tr>
                <td><input id="mychk" type="checkbox" name="mychk"/></td>
                <td>${dis.id }</td>
                <td>${dis.name }</td>
    
            </tr>
         </c:forEach>
         
        </table>
    
      </body>

    2、js

    <script type="text/javascript" src="js/jquery-1.8.3.js"></script>
        
        <script type="text/javascript">
            /* $("#all").click(function(){
                alert("11111111");
                if(this.checked){
                    alert("2222");
                    $("mychk").prop("checked",true);
                }else{
                    $("mychk").prop("checked",false);
                }
            
            }); */
            function chk(){
                var all = document.getElementById("all");
                var mychk = document.getElementsByName("mychk");
                alert("mychk长度=="+mychk.length);
                if(all.checked==true){
                    alert("all.checked==true全选");
                    if(mychk.length){
                        for(var i=0;i<mychk.length;i++){
                            mychk[i].checked = true;
                        }
                    
                    }
                    mychk.chcked=true;
                }else{
                    alert("all.checked==false全不选");
                    if(mychk.length){
                        for(var i=0;i<mychk.length;i++){
                            mychk[i].checked = false;
                        }
                    
                    }
                }
                
            }   
        </script>
  • 相关阅读:
    树莓派成长日记03
    一些特殊文字的过滤Private Use Area:E000F8FF
    MongoDb 相关
    SQL 相关技术点收集贴
    正则表达式提取文本的日期
    MVC 相关技术点收集贴
    使用 json2.js注意点
    C#画图 GDI+
    PHP模拟POST,验证页面的返回状态
    EF-Entity Framework 相关技术点收集贴
  • 原文地址:https://www.cnblogs.com/sincoolvip/p/6518645.html
Copyright © 2020-2023  润新知