jQuery实现多个表格的全选复选框功能

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery实现多个表格的全选复选框功能</title>
    <!-- 引入jQuery库 -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background-color: #f5f5f5;
            padding: 20px;
            line-height: 1.6;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 30px;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }
        
        .intro {
            margin-bottom: 30px;
        }
        
        /* 表格样式 */
        .table-container {
            margin-bottom: 40px;
        }
        
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 15px;
        }
        
        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        
        th {
            background-color: #f8f9fa;
            font-weight: bold;
        }
        
        tr:hover {
            background-color: #f1f1f1;
        }
        
        /* 复选框样式 */
        .checkbox-cell {
            width: 40px;
            text-align: center;
        }
        
        /* 不确定状态样式 */
        .indeterminate {
            position: relative;
        }
        
        .indeterminate:before {
            content: "";
            display: block;
            position: absolute;
            top: 50%;
            left: 50%;
            width: 12px;
            height: 2px;
            margin-top: -1px;
            margin-left: -6px;
            background-color: #333;
        }
        
        /* 操作按钮区域 */
        .action-bar {
            margin: 20px 0;
            padding: 15px;
            background-color: #f8f9fa;
            border-radius: 4px;
        }
        
        button {
            padding: 8px 15px;
            margin-right: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        
        button:hover {
            background-color: #45a049;
        }
        
        /* 响应式设计 */
        @media (max-width: 768px) {
            th, td {
                padding: 8px 10px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>jQuery实现多个表格的全选复选框功能</h1>
        
        <div class="intro">
            <p>这个示例演示了如何使用jQuery实现多个表格的全选复选框功能。更多Web开发资源可以访问:</p>
            <p>全选功能是数据表格中常见的交互需求,可以方便用户批量操作数据。</p>
        </div>
        
        <!-- 第一个表格 -->
        <div class="table-container">
            <h2>用户列表</h2>
            <table id="userTable">
                <thead>
                    <tr>
                        <th class="checkbox-cell">
                            <input type="checkbox" id="selectAllUsers" class="select-all">
                        </th>
                        <th>ID</th>
                        <th>用户名</th>
                        <th>邮箱</th>
                        <th>注册时间</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td class="checkbox-cell"><input type="checkbox" class="row-checkbox"></td>
                        <td>1</td>
                        <td>张三</td>
                        <td>zhangsan@example.com</td>
                        <td>2023-01-15</td>
                    </tr>
                    <tr>
                        <td class="checkbox-cell"><input type="checkbox" class="row-checkbox"></td>
                        <td>2</td>
                        <td>李四</td>
                        <td>lisi@example.com</td>
                        <td>2023-02-20</td>
                    </tr>
                    <tr>
                        <td class="checkbox-cell"><input type="checkbox" class="row-checkbox"></td>
                        <td>3</td>
                        <td>王五</td>
                        <td>wangwu@example.com</td>
                        <td>2023-03-10</td>
                    </tr>
                    <tr>
                        <td class="checkbox-cell"><input type="checkbox" class="row-checkbox"></td>
                        <td>4</td>
                        <td>赵六</td>
                        <td>zhaoliu@example.com</td>
                        <td>2023-04-05</td>
                    </tr>
                </tbody>
            </table>
            <div class="action-bar">
                <button id="deleteUsers">删除选中用户</button>
                <button id="exportUsers">导出选中用户</button>
            </div>
        </div>
        
        <!-- 第二个表格 -->
        <div class="table-container">
            <h2>产品列表</h2>
            <table id="productTable">
                <thead>
                    <tr>
                        <th class="checkbox-cell">
                            <input type="checkbox" id="selectAllProducts" class="select-all">
                        </th>
                        <th>ID</th>
                        <th>产品名称</th>
                        <th>价格</th>
                        <th>库存</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td class="checkbox-cell"><input type="checkbox" class="row-checkbox"></td>
                        <td>101</td>
                        <td>智能手机</td>
                        <td>¥2999</td>
                        <td>120</td>
                    </tr>
                    <tr>
                        <td class="checkbox-cell"><input type="checkbox" class="row-checkbox"></td>
                        <td>102</td>
                        <td>笔记本电脑</td>
                        <td>¥5999</td>
                        <td>45</td>
                    </tr>
                    <tr>
                        <td class="checkbox-cell"><input type="checkbox" class="row-checkbox"></td>
                        <td>103</td>
                        <td>平板电脑</td>
                        <td>¥2499</td>
                        <td>78</td>
                    </tr>
                    <tr>
                        <td class="checkbox-cell"><input type="checkbox" class="row-checkbox"></td>
                        <td>104</td>
                        <td>智能手表</td>
                        <td>¥1299</td>
                        <td>156</td>
                    </tr>
                    <tr>
                        <td class="checkbox-cell"><input type="checkbox" class="row-checkbox"></td>
                        <td>105</td>
                        <td>无线耳机</td>
                        <td>¥599</td>
                        <td>230</td>
                    </tr>
                </tbody>
            </table>
            <div class="action-bar">
                <button id="deleteProducts">删除选中产品</button>
                <button id="updateStock">更新库存</button>
            </div>
        </div>
        
        <!-- 代码示例区域 -->
        <div class="code-example">
            <h3>jQuery实现全选功能核心代码:</h3>
            <pre>
// 初始化全选功能
function initSelectAll() {
    // 为每个表格绑定事件
    $('.select-all').each(function() {
        const $selectAll = $(this);
        const tableId = $selectAll.closest('table').attr('id');
        const $rowCheckboxes = $('#' + tableId).find('.row-checkbox');
        
        // 表头全选复选框点击事件
        $selectAll.on('click', function() {
            const isChecked = $(this).prop('checked');
            $rowCheckboxes.prop('checked', isChecked);
            
            // 移除不确定状态
            if (isChecked) {
                $(this).prop('indeterminate', false);
            }
        });
        
        // 行复选框点击事件
        $rowCheckboxes.on('click', function() {
            const allChecked = $rowCheckboxes.length === $rowCheckboxes.filter(':checked').length;
            const someChecked = $rowCheckboxes.filter(':checked').length > 0;
            
            $selectAll.prop('checked', allChecked);
            $selectAll.prop('indeterminate', someChecked && !allChecked);
        });
    });
}

// 初始化页面
$(document).ready(function() {
    initSelectAll();
    
    // 示例操作按钮事件
    $('#deleteUsers').on('click', function() {
        const selectedIds = [];
        $('#userTable .row-checkbox:checked').each(function() {
            selectedIds.push($(this).closest('tr').find('td:eq(1)').text());
        });
        
        if (selectedIds.length > 0) {
            alert('将删除用户ID: ' + selectedIds.join(', '));
        } else {
            alert('请先选择要删除的用户');
        }
    });
    
    // 更多示例代码...
});


            </pre>
        </div>
    </div>
    
    <script>
        // 初始化全选功能
        function initSelectAll() {
            // 为每个表格绑定事件
            $('.select-all').each(function() {
                const $selectAll = $(this);
                const tableId = $selectAll.closest('table').attr('id');
                const $rowCheckboxes = $('#' + tableId).find('.row-checkbox');
                
                // 表头全选复选框点击事件
                $selectAll.on('click', function() {
                    const isChecked = $(this).prop('checked');
                    $rowCheckboxes.prop('checked', isChecked);
                    
                    // 移除不确定状态
                    if (isChecked) {
                        $(this).prop('indeterminate', false);
                    }
                });
                
                // 行复选框点击事件
                $rowCheckboxes.on('click', function() {
                    updateSelectAllState($selectAll, $rowCheckboxes);
                });
                
                // 初始状态检查
                updateSelectAllState($selectAll, $rowCheckboxes);
            });
        }
        
        // 更新表头复选框状态
        function updateSelectAllState($selectAll, $rowCheckboxes) {
            const allChecked = $rowCheckboxes.length === $rowCheckboxes.filter(':checked').length;
            const someChecked = $rowCheckboxes.filter(':checked').length > 0;
            
            $selectAll.prop('checked', allChecked);
            $selectAll.prop('indeterminate', someChecked && !allChecked);
        }
        
        // 初始化页面
        $(document).ready(function() {
            initSelectAll();
            
            // 删除用户按钮事件
            $('#deleteUsers').on('click', function() {
                const selectedIds = [];
                $('#userTable .row-checkbox:checked').each(function() {
                    selectedIds.push($(this).closest('tr').find('td:eq(1)').text());
                });
                
                if (selectedIds.length > 0) {
                    alert('将删除用户ID: ' + selectedIds.join(', '));
                } else {
                    alert('请先选择要删除的用户');
                }
            });
            
            // 导出用户按钮事件
            $('#exportUsers').on('click', function() {
                const count = $('#userTable .row-checkbox:checked').length;
                if (count > 0) {
                    alert('将导出 ' + count + ' 个用户数据');
                } else {
                    alert('请先选择要导出的用户');
                }
            });
            
            // 删除产品按钮事件
            $('#deleteProducts').on('click', function() {
                const selectedIds = [];
                $('#productTable .row-checkbox:checked').each(function() {
                    selectedIds.push($(this).closest('tr').find('td:eq(1)').text());
                });
                
                if (selectedIds.length > 0) {
                    alert('将删除产品ID: ' + selectedIds.join(', '));
                } else {
                    alert('请先选择要删除的产品');
                }
            });
            
            // 更新库存按钮事件
            $('#updateStock').on('click', function() {
                const count = $('#productTable .row-checkbox:checked').length;
                if (count > 0) {
                    alert('将更新 ' + count + ' 个产品的库存');
                } else {
                    alert('请先选择要更新库存的产品');
                }
            });
            
            // 控制台输出信息
            console.log('表格全选功能已初始化');
        });
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值