jQuery全选反选操作 jQuery实现全选反选操作案例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

jQuery全选反选操作 jQuery实现全选反选操作案例

来干了这碗代码   2021-08-10 我要评论
想了解jQuery实现全选反选操作案例的相关内容吗,来干了这碗代码在本文为您仔细讲解jQuery全选反选操作的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:jQuery,全选,反选,下面大家一起来学习吧。

全选+反选

可根据控制台结合查看结果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>过滤选择器</title>
    <script src="jquery-3.2.1.min.js"></script>
</head>

<body>

    <table border="1">
        <tr>
            <td><input type="checkbox" value="1"></td>
            <td>剑圣</td>
            <td>450</td>
        </tr>
        <tr>
            <td><input type="checkbox" value="2"></td>
            <td>剑豪</td>
            <td>6300</td>
        </tr>
        <tr>
            <td><input type="checkbox" value="3"></td>
            <td>剑姬</td>
            <td>6300</td>
        </tr>
        <tr>
            <td><input type="checkbox" value="4"></td>
            <td>剑魔</td>
            <td>6300</td>
        </tr>
    </table>
 
    <input type="button" value="点击选择使用第一个" id="firstBtn">
    <input type="button" value="点击选择使用最后一个" id="lastBtn">
    <input type="button" value="全选适用于批量删除" id="allBtn">
    <input type="button" value="查看已选中的" id="checkBtn">
    <input type="button" value="查看未选中的" id="nocheckBtn">
    <input type="button" value="反选" id="overBtn">
    <input type="button" value="反选的升级版" id="overBtn1">

<script>

        $(function() {
            //jQuery 使用过滤选择器 达到 奇偶数变色
            $("table tr:even").css('background-color','pink');
            $("table tr:odd").css('background-color','blue');
            // 
            
            // 拿去第一个
            $("#firstBtn").click(function() {
                var first = $("table tr:first").html();
                console.log(first);  
            })

             // 拿取最后一个
             $("#lastBtn").click(function() {
                var last = $("table tr:last").text();
                console.log(last);  
            })
            // 全选 ---- 用来批量删除
            $("#allBtn").click(function() { 
                // 思路找出所有 checkbox的 td 进行遍历 选中即可 
                $.each($("table tr td>input"), function(index, value) {
                  //  console.log(index);   
                  //  console.log(value);
                    console.log($(this).val()); // 遍历取值
                    $(this).prop('checked',true); // 全选
                })

            }) 
            // 点击查看已经选中的
            $("#checkBtn").click(function() { 
                // 使用过滤选择器 可以选中 :
                $("table tr td>input:checked")
                $.each($("table tr td>input:checked"), function(index, value) {
                    
                    console.log($(this).val()); // 遍历取值
                    
                })
            
            }) 
            // 点击查看未选中的
            $("#nocheckBtn").click(function() { 
                console.log($("table tr td>input:not(:checked)"))
            })
            // 反选
            $("#overBtn").click(function() { 
                $.each($("table tr td>input"), function(index, value) {
                    var istrue =$(this).prop("checked");
                    //console.log(value.checked = !value.checked); // 遍历取值
                   if(istrue){
                        $(this).prop("checked",false);
                   } else{
                    $(this).prop("checked",true);
                   }
                })   
            })     
            // 升级版的全/反选
            $("#overBtn1").click(function() { 
                $.each($("table tr td>input"), function(index, value){
                $(this).prop("checked",!$(this).prop("checked"))
                })
            })
    })  
</script>

</body>
</html>

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们