jQuery实现全选、反选、取消

本文介绍了如何使用jQuery实现全选、反选和取消选择的功能。通过一个简单的HTML表格和按钮,结合jQuery事件绑定,展示了具体实现这些功能的代码示例,包括全选、反选的多种实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求:经常看到网页上有全选、反选等等按钮,我们用jQuery实现。

DOM实现参照:DOM实现全选、反选、取消

一个简单的html表格可以写出来,同时让按钮绑定点击事件:

    <input type="button" value="全选" onclick="allPick();">
    <input type="button" value="反选" onclick="reservePick();">
    <input type="button" value="取消" onclick="cancel();">


    <table border="1" >
        <thead>
            <tr>
                <th>选项</th>
                <th>1</th>
                <th>2</th>
            </tr>
        </thead>
        <tbody id="tb1">
            <tr>
                <td><input type="checkbox"></td>
                <td>1,1</td>
                <td>1,2</td>
            </tr>
            <tr>
                <td><input type="checkbox"></td>
                <td>2,1</td>
                <td>2,2</td>
            </tr>
            <tr>
                <td><input type="checkbox"></td>
                <td>3,1</td>
                <td>3,2</td>
            </tr>
        </tbody>
    </table>

接下来是用JQuery实现全选反选取消:

反选的实现有3种方法:参照下面代码里的注释

    <script src="jquery-3.3.1.js"></script>
    <script>
        function allPick() {
            console.log(11111);
            $('#tb1 :checkbox').prop('checked',true);
        }
        function cancel() {
            $('#tb1 :checkbox').prop('checked',false);
        }
        function reservePick() {
            $('#tb1 :checkbox').each(function () {
                // 这里的this是dom对象,所以可以直接用this.checked来设置值
                // 方法1:
                if (this.checked){
                    this.checked = false;
                }else {
                    this.checked = true;
                }

                // jQuery写法,方法2:用prop方法,有一个参数是获取值,有2个参数是设置值
                if ($(this).prop('checked')){
                    $(this).prop('checked', false);
                } else $(this).prop('checked', true);

                // 方法3,用JavaScript三元式语法来直接设置值
                $(this).prop('checked', $(this).prop('checked')?false:true);
            })
        }
    </script>

完整网页面代码(html):记得把JQuery包引入并且复制到同一个文件夹

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <input type="button" value="全选" onclick="allPick();">
    <input type="button" value="反选" onclick="reservePick();">
    <input type="button" value="取消" onclick="cancel();">


    <table border="1" >
        <thead>
            <tr>
                <th>选项</th>
                <th>1</th>
                <th>2</th>
            </tr>
        </thead>
        <tbody id="tb1">
            <tr>
                <td><input type="checkbox"></td>
                <td>1,1</td>
                <td>1,2</td>
            </tr>
            <tr>
                <td><input type="checkbox"></td>
                <td>2,1</td>
                <td>2,2</td>
            </tr>
            <tr>
                <td><input type="checkbox"></td>
                <td>3,1</td>
                <td>3,2</td>
            </tr>
        </tbody>
    </table>

    <script src="jquery-3.3.1.js"></script>
    <script>
        function allPick() {
            console.log(11111);
            $('#tb1 :checkbox').prop('checked',true);
        }
        function cancel() {
            $('#tb1 :checkbox').prop('checked',false);
        }
        function reservePick() {
            $('#tb1 :checkbox').each(function () {
                // 这里的this是dom对象,所以可以直接用this.checked来设置值
                // 方法1:
                if (this.checked){
                    this.checked = false;
                }else {
                    this.checked = true;
                }

                // jQuery写法,方法2:用prop方法,有一个参数是获取值,有2个参数是设置值
                if ($(this).prop('checked')){
                    $(this).prop('checked', false);
                } else $(this).prop('checked', true);

                // 方法3,用JavaScript三元式语法来直接设置值
                $(this).prop('checked', $(this).prop('checked')?false:true);
            })
        }
    </script>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值