jQuery 之 多选反选取消实例

本文介绍了一个使用HTML创建表格并利用jQuery实现全选、反选和取消选择功能的例子。通过简单的示例代码展示了如何操作表格中的复选框。

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

代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="jquery3.js"></script>
</head>
<body>
    <table border="1">
        <thead>
        <tr>
            <th style="text-align: center">选择</th>
            <th style="text-align: center">IP</th>
            <th style="text-align: center">端口</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td><input type="checkbox"></td>
            <td>1.1.1.1</td>
            <td>80</td>
        </tr>
        <tr>
            <td><input type="checkbox"></td>
            <td>2.2.2.2</td>
            <td>80</td>
        </tr>
        <tr>
            <td><input type="checkbox"></td>
            <td>3.3.3.3</td>
            <td>80</td>
        </tr>
        </tbody>
    </table>
    <input type="button" value="全选" onclick="checkAll()">
    <input type="button" value="反选" onclick="reverseAll()">
    <input type="button" value="取消" onclick="cancleAll()">
    <script>
        function checkAll() {
//            把checkbox的checked值设置为true
            $(':checkbox').prop('checked',true)
//            $(':checkbox').prop('checked')为获取值
        }
        function cancleAll() {
//            把checkbox的checked值设置为false
            $(':checkbox').prop('checked',false)
//            $(':checkbox').prop('checked')为获取值
        }
        function reverseAll() {
            $(':checkbox').each(function (k) {
                // k当前索引
                // this,DOM元素,当前循环的元素$(this)
                if(this.checked)
                {
                    this.checked = false;
                }
                else{
                    this.checked = true;
                }
            })
        }
    </script>

</body>
</html>

展示:

jQuery 之 多选反选取消实例

转载于:https://blog.51cto.com/12965094/2372992

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值