輸入框輸入字符 → 表格行即時隱藏/顯示 → 支持模糊匹配、大小寫不敏感、多列搜索。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery 表格實時過濾</title>
<style>
table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background: #333; color: white; }
tr:nth-child(even) { background: #fff38f; } /* 斑馬紋 */
tr:nth-child(odd) { background: #ffffee; }
.highlight { background: #ff6500; color: #fff; } /* 高亮類 */
</style>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js"></script>
</head>
<body>
<div style="margin-bottom: 1rem;">
🔍 篩選:<input id="filterInput" type="text" placeholder="輸入關鍵字...">
</div>
<table id="dataTable">
<thead>
<tr><th>姓名</th><th>性別</th><th>暫住地</th></tr>
</thead>
<tbody>
<tr><td>SYJ</td><td>男</td><td>河北秦皇島市</td></tr>
<tr><td>李四</td><td>女</td><td>北京北京市</td></tr>
<tr><td>王五</td><td>男</td><td>河北秦皇島市</td></tr>
<tr><td>趙六</td><td>男</td><td>河北唐山市</td></tr>
<tr><td>張三</td><td>男</td><td>內蒙古呼和浩特市</td></tr>
<tr><td>王六</td><td>女</td><td>內蒙古包頭市</td></tr>
<tr><td>二狗</td><td>男</td><td>北京北京市</td></tr>
<tr><td>李字</td><td>女</td><td>河北秦皇島市</td></tr>
<tr><td>二蛋</td><td>男</td><td>東北遼寧省</td></tr>
</tbody>
</table>
<script>
$(function() {
const $rows = $('#dataTable tbody tr');
const $input = $('#filterInput');
$input.on('keyup', function() {
const keyword = $(this).val().toLowerCase().trim();
if (!keyword) {
$rows.show(); // 空關鍵字顯示所有
return;
}
$rows.hide().filter(function() {
// 遍歷行內所有單元格文本,任一匹配即顯示
return $(this).text().toLowerCase().indexOf(keyword) > -1;
}).show();
});
});
</script>
</body>
</html>
$rows.hide().filter(fn) 保留滿足條件的行.show() 顯示匹配行// 當前:任意單元格包含關鍵字即匹配(OR 邏輯)
$(this).text().toLowerCase().indexOf(keyword) > -1
// 進階:精確匹配整詞
new RegExp('\\b' + keyword + '\\b', 'i').test($(this).text())
// 進階:指定列搜索(如只搜姓名列)
$(this).find('td:eq(0)').text().toLowerCase().indexOf(keyword) > -1
// 防抖:輸入停止 150ms 後才執行過濾
$input.on('input', $.debounce(150, function() { ... }));
// 大數據量(>1000 行)建議:
// 1. 虛擬滾動只渲染可視區
// 2. Web Worker 異步計算匹配索引
// 3. 虛擬 DOM 差異更新
$input.on('keyup', function() {
const keywords = $(this).val().toLowerCase().trim().split(/\s+/).filter(Boolean);
$rows.hide().filter(function() {
const rowText = $(this).text().toLowerCase();
return keywords.every(kw => rowText.includes(kw)); // 所有關鍵字都需匹配
}).show();
});
function highlight($row, keyword) {
$row.find('td').each(function() {
const $cell = $(this);
const text = $cell.text();
if (text.toLowerCase().includes(keyword)) {
// 保留原有 HTML 結構,僅包裹文本節點
$cell.html(text.replace(
new RegExp(`(${keyword})`, 'gi'),
'<mark>$1</mark>'
));
}
});
}
<select id="colSelect">
<option value="all">全部列</option>
<option value="0">姓名</option>
<option value="1">性別</option>
<option value="2">暫住地</option>
</select>
// 過濾邏輯中讀取選中列索引
const colIdx = $('#colSelect').val();
const $targetCells = colIdx === 'all' ? $row.find('td') : $row.find(`td:eq(${colIdx})`);
| 框架 | 實現方式 | 核心API |
|---|---|---|
| Vue 3 | computed 過濾數據源 | v-for + filter() |
| React | useMemo 緩存過濾結果 | .filter() + useState |
| Alpine.js | x-show 綁定匹配邏輯 | x-data + x-show |
| 原生 JS | input 事件 + DOM 遍歷 | querySelectorAll + textContent |
jQuery 表格過濾的精髓在於 .hide().filter(fn).show() 鏈式模式——聲明式描述「什麼行該顯示」,而非命令式控制每行顯隱。理解這種集合操作思維,能幫助你在任何框架中寫出簡潔高效的列表過濾邏輯。