jQuery的全选反选

基于jq的表格全选反选
页面样式:

<!DOCTYPE html>
<html lang="cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
   //注意引用jquery
    <script src="jq/jquery-3.4.0.js"></script>
    <style>
        * {
     
     
            padding: 0;
            margin: 0;
        }

        .wrap {
     
     
            width: 300px;
            margin: 100px auto 0;
        }

        table {
     
     
            border-collapse: collapse;
            border-spacing: 0;
            border: 1px solid #c0c0c0;
            width: 300px;
        }

        th,
        td {
     
     
            border: 1px solid #d0d0d0;
            color: #404060;
            padding: 10px;
        }

        th {
     
     
            background-color: #ff3040;
            font: bold 16px "微软雅黑";
            color: #fff;
        }

        td {
     
     
            font: 14px "微软雅黑";
        }

        tbody tr {
     
     
            background-color: #f0f0f0;
        }

        tbody tr:hover {
     
     
            cursor: pointer;
            background-color: #fafafa;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <table>
            <thead>
                <tr>
                    <th>
                        <input type="checkbox" id="j_cbAll" />
                    </th>
                    <th>商品</th>
                    <th>价钱</th>
                </tr>
            </thead>
            <tbody id="j_tb">
                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>iPhone8</td>
                    <td>8000</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>iPad Pro</td>
                    <td>5000</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>iPad Air</td>
                    <td>2000</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>Apple Watch</td>
                    <td>2000</td>
                </tr>

            </tbody>
        </table>
        <input type="button" value="  反 选  " id="btn">
    </div>
    <script>
     
    </script>
</body>

</html>

核心jq代码

<script>
        // 全选
        $('thead input').click(function () {
    
    
            console.log($(this).is($(':checked')));
            if ($(this).prop('checked')) {
    
    
                $('#j_tb>tr>td>input').prop('checked', true);
            } else {
    
    
                $('#j_tb>tr>td>input').prop('checked', false);
            }
        })
        反选
        $('#btn').click(function () {
    
    
            // 需要遍历
            $('#j_tb>tr>td>input').each(function () {
    
    
                if ($(this).prop('checked')) {
    
    
                    $(this).prop('checked', false);
                } else {
    
    
                    $(this).prop('checked', true);
                }
            })
        })
  
    </script>

或者:

  <script>
        // 全选
        $('thead input').click(function () {
    
    
            console.log($(this).is($(':checked')));
            if ($(this).prop('checked')) {
    
    
                $('#j_tb>tr>td>input').prop('checked', true);
            } else {
    
    
                $('#j_tb>tr>td>input').prop('checked', false);
            }
        })
        反选
        $('#btn').click(function () {
    
    
            // 需要遍历
            $('#j_tb>tr>td>input').each(function () {
    
    
            //这里用的是is()
                if ($(this).is($(':checked'))) {
    
    
                    $(this).prop('checked', false);
                } else {
    
    
                    $(this).prop('checked', true);
                }
            })
        })
    </script>

猜你喜欢

转载自blog.csdn.net/weixin_48549175/article/details/112859611