全選+反選
可根據控制臺結合查看結果

<!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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/292735.html
標籤:其他
