jq全选全不选、反选和删除(入门详细)

css代码如下

这里需要引用jjq文件,网络不好的话可以下载下来  下载地址Download jQuery | jQuery

<head lang="en">
    <meta charset="UTF-8">
    <title>案例</title>
    <style>
        #form {
            width: 480px;
            margin: 30px auto;
            border: 1px solid #eee;
            border-radius: 5px;
            padding: 10px;
            line-height: 30px;
            position: relative;
        }

        button {
            position: absolute;
            right: 10px;
            bottom: 10px;
        }

        #tab {
            width: 500px;
            margin: 30px auto;
            border-collapse: collapse;
        }

        th,
        td {
            border: 1px solid #000;
            padding: 5px;
        }

        tbody tr td:first-child {
            text-align: center;
        }

        /*input[type]  属性选择器  选择input标签,并且有type属性input标签*/
        /*input[type = "checkbox"]  选择有type属性并且值为checkbox的input标签*/

        input[type="checkbox"] {
            width: 15px;
            height: 15px;
        }

        #div1 {
            position: relative;
            width: 480px;
            padding: 10px;
            margin: 0 auto;
        }
    </style>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.js"></script>
</head>

html代码如下

<div id="form">
        请输入姓名: <input type="text" id="name"> <br> 
        请输入性别: <input type="radio" id="sex" name="sex" checked>男 
                   <input type="radio" name="sex">女<br> 
        请输入年龄: <input type="text" id="age">
        <button>添加到表格</button>
    </div>
    <table id="tab">
        <thead>
            <tr>
                <th width="20%"><input type="checkbox" id="all">全选</th>
                <th>姓名</th>
                <th>性别</th>
                <th>年龄</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><input type="checkbox"></td>
                <td>张三</td>
                <td>女</td>
                <td>88</td>
            </tr>
            <tr>
                <td><input type="checkbox"></td>
                <td>李四</td>
                <td>男</td>
                <td>18</td>
            </tr>
            <tr>
                <td><input type="checkbox"></td>
                <td>王五</td>
                <td>女</td>
                <td>1</td>
            </tr>
        </tbody>
    </table>
    <div id="div1">
        <button>删除所选行</button>
    </div>

js代码如下

全选全不选、反选和删除

<script>
        $("#form button").click(function () {
            //不能为空
            if ($("input:eq(0)").val() == '' && $("input:eq(3)").val() == '') {
                alert('请输入账号和密码');
                return false;
            };
            //添加内容
            $("tbody").append(`
            <tr>
                <td><input type="checkbox"></td>
                <td>${$("input:eq(0)").val()}</td >
                <td>${$("input:eq(1)").prop("checked") ? '男' : '女'}</td>
                <td>${$("input:eq(3)").val()}</td>
            </tr >
            `);
            // 添加完毕之后清零
            $("input:eq(0)").val('');
            $("input:eq(3)").val('');
            $(':radio:eq(0)').prop('checked', true);
            $("#all").prop("checked", false);
        });

        //全选
        $("#all").click(function () {
            $("tbody input").prop("checked", $(this).prop("checked"));
        });
        //全不选
        $('tbody').on('click', 'input', function () {
            $('tbody input').length == $('tbody input:checked').length ? 
            $('#all').prop('checked', true) : $('#all').prop('checked', false);
        });

        //删除
        $("#div1 button").click(function () {
            $("tbody input:checked").parents('tr').remove();
            $("#all").prop("checked", false);
        });
    </script>

 整体代码如下

<!DOCTYPE html>
<html>

<head lang="en">
    <meta charset="UTF-8">
    <title>案例</title>
    <style>
        #form {
            width: 480px;
            margin: 30px auto;
            border: 1px solid #eee;
            border-radius: 5px;
            padding: 10px;
            line-height: 30px;
            position: relative;
        }

        button {
            position: absolute;
            right: 10px;
            bottom: 10px;
        }

        #tab {
            width: 500px;
            margin: 30px auto;
            border-collapse: collapse;
        }

        th,
        td {
            border: 1px solid #000;
            padding: 5px;
        }

        tbody tr td:first-child {
            text-align: center;
        }

        /*input[type]  属性选择器  选择input标签,并且有type属性input标签*/
        /*input[type = "checkbox"]  选择有type属性并且值为checkbox的input标签*/

        input[type="checkbox"] {
            width: 15px;
            height: 15px;
        }

        #div1 {
            position: relative;
            width: 480px;
            padding: 10px;
            margin: 0 auto;
        }
    </style>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.js"></script>
</head>

<body>
    <div id="form">
        请输入姓名: <input type="text" id="name"> <br> 请输入性别: <input type="radio" id="sex" name="sex" checked>男 <input
            type="radio" name="sex">女<br> 请输入年龄: <input type="text" id="age">
        <button>添加到表格</button>
    </div>
    <table id="tab">
        <thead>
            <tr>
                <th width="20%"><input type="checkbox" id="all">全选</th>
                <th>姓名</th>
                <th>性别</th>
                <th>年龄</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><input type="checkbox"></td>
                <td>张三</td>
                <td>女</td>
                <td>88</td>
            </tr>
            <tr>
                <td><input type="checkbox"></td>
                <td>李四</td>
                <td>男</td>
                <td>18</td>
            </tr>
            <tr>
                <td><input type="checkbox"></td>
                <td>王五</td>
                <td>女</td>
                <td>1</td>
            </tr>
        </tbody>
    </table>
    <div id="div1">
        <button>删除所选行</button>
    </div>
    <script>
        $("#form button").click(function () {
            //不能为空
            if ($("input:eq(0)").val() == '' && $("input:eq(3)").val() == '') {
                alert('请输入账号和密码');
                return false;
            };
            //添加内容
            $("tbody").append(`
            <tr>
                <td><input type="checkbox"></td>
                <td>${$("input:eq(0)").val()}</td >
                <td>${$("input:eq(1)").prop("checked") ? '男' : '女'}</td>
                <td>${$("input:eq(3)").val()}</td>
            </tr >
            `);
            // 添加完毕之后清零
            $("input:eq(0)").val('');
            $("input:eq(3)").val('');
            $(':radio:eq(0)').prop('checked', true);
            $("#all").prop("checked", false);
        });

        //全选
        $("#all").click(function () {
            $("tbody input").prop("checked", $(this).prop("checked"));
        });
        //全不选
        $('tbody').on('click', 'input', function () {
            $('tbody input').length == $('tbody input:checked').length ? $('#all').prop('checked', true) : $('#all').prop('checked', false);
        });

        //删除
        $("#div1 button").click(function () {
            $("tbody input:checked").parents('tr').remove();
            $("#all").prop("checked", false);
        });
    </script>

</html>

如有错误欢迎私信或评论留言

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值