zoukankan      html  css  js  c++  java
  • Js全选 添加和单独删除

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js"></script>
        <style>
            *{
                margin: 0;
                padding: 0;
            }
            table{
                border-collapse: collapse;
                margin-top:50px;
            }
            th,td{
                 80px;
                height: 40px;
                line-height: 40px;
                text-align: center;
                border:1px solid #000;
            }
        </style>
    </head>
    <body>
    <input type="text" value="zs">姓名
    <input type="text" value="20">年龄
    <input type="text" value="">性别
    <button type="button" class="tj">提交</button><br/>
    <table>
        <thead>
        <tr>
            <th><input type="checkbox" class="checkAll">全选</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <!--<tr>
            <td><input type="checkbox"></td>
            <td>zs</td>
            <td>20</td>
            <td>nan</td>
            <td><button>删除</button></td>
        </tr>-->
        </tbody>
    </table>
    <script>
        //计数器
        var n=0;
        $(".tj").click(function(){
            var name=$("input").eq(0).val();
            var age=$("input").eq(1).val();
            var sex=$("input").eq(2).val();
            var add="<tr><td><input type='checkbox'></td><td>"+name+"</td><td>"+age+"</td><td>"+sex+"</td><td><button>删除</button></td></tr>"
            $("table").append(add);
        });
        /*$("tbody button").click(function(){
         alert(0)
         })*/
        $("tbody").on("click","button",function(){
            $(this).parents("tr").remove();
        });
        //点击全选,下面的全部选择
        $(".checkAll").click(function(){
            //console.log($(this).prop("checked"));
            if($(this).prop("checked")==true){
                $("tbody input").prop("checked",true);
            }else{
                $("tbody input").prop("checked",false);
            }
        });
        //下面的选择点击
        $("tbody").on("click","input",function(){
            if($(this).prop("checked")==true){
                n++;
            }else{
                n--;
            }
            if(n==$("tbody input").length){
                $(".checkAll").prop("checked",true);
            }else{
                $(".checkAll").prop("checked",false);
            }
        });
    
    
    </script>
    </body>
    </html>
  • 相关阅读:
    剑指OFFER之包含min函数的栈
    剑指OFFER之二叉树的镜像
    关于【最长递增子序列(LIS)】
    题目1113:二叉树
    剑指OFFER之字符串的排列
    题目1120:全排列
    题目1460:Oil Deposit
    题目1459:Prime ring problem
    剑指OFFER之二叉树中和为某一值的路径
    python 线程、进程
  • 原文地址:https://www.cnblogs.com/yangzhewoaini/p/7645854.html
Copyright © 2011-2022 走看看