1. 程式人生 > 程式設計 >jQuery實現全選反選操作案例

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."></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"> <inphttp://www.cppcns.comut type="button" value="反選" id="overBtn"> <input type="button" value="反選的升級版" id="overBtn1"> <script> $(function() { //jQuery 使用過濾選擇器 達到 奇偶數變色 $("table tr:even").('background-color','pink'); $("table tr:odd").css('background-color','blue'); // // 拿去第一個 $("#firstBtn").click(function() { var first = $("table tr:first").html(); qmDtyHk
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"),value) { console.log($(this).val()); // 遍歷取值 }) }) // 點選檢視未選中的 $("#nocheckBtn").click(function() { console.log($("table tr td>input:not(:checked)")) }) // 反選 $("#overBtn").click(function() { $.each($("table tr td>input"),value) { var istrue =$(this).prop("checked"); //console.log(value.checked = !value.checked); // 遍歷取值 if(istrue){ $(thttp://www.cppcns.comhis).prop("checked",false); } else{ $(this).prop("checked",true); } }) }) // 升級版的全/反選 $("#overBtn1").click(function() { $.each($("table tr td>input"),value){ $(this).prop("checked",!$(this).prop("checked")) }) }) }) </script> </body> </html>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。