jQuery实现全选反选操作案例

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了jQuery实现全选反选操作案例脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例为大家分享了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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本宝典。

脚本宝典总结

以上是脚本宝典为你收集整理的jQuery实现全选反选操作案例全部内容,希望文章能够帮你解决jQuery实现全选反选操作案例所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。