對(duì)于表格來說,當(dāng)數(shù)據(jù)比較多的時(shí)候,我們無法一頁一頁的查找,這樣我們就可以進(jìn)行篩選操作,這篇文章主要為大家詳細(xì)介紹了基于jquery實(shí)現(xiàn)表格內(nèi)容篩選功能的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
當(dāng)表格內(nèi)的數(shù)據(jù)較多時(shí),我們無法一頁一頁的查找,這時(shí)可以通過一個(gè)搜索框來實(shí)現(xiàn)搜索。
對(duì)于這個(gè)搜素框,我們?yōu)榱烁玫捏w驗(yàn)可以利用keyup事件實(shí)現(xiàn)在用戶輸入的時(shí)候就開始篩選,而不是填完以后點(diǎn)擊搜索按鈕再執(zhí)行。
效果圖:
實(shí)現(xiàn)代碼:
<html>
<head>
<meta charset="utf-8" />
<script src="jquery-1.3.2.min.js"></script>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script>
$(function () {
$("tr.parent").click(function () {
$(this)
.siblings('.child_'+this.id).toggle();
});
$("tr.parent").addClass("selected");
$("#searchbox").keyup(function () {
$("table tbody tr").hide()
.filter(":contains('"+($(this).val())+"')").show();//filter和contains共同來實(shí)現(xiàn)了這個(gè)功能。
}).keyup();
});
</script>
<title></title>
</head>
<body>
<label>篩選</label>
<input type="text" id="searchbox"/>
<table>
<thead>
<tr><td>姓名</td><td>性別</td><td>暫住地</td></tr>
</thead>
<tbody>
<tr class="parent" id="row_01"><td>前臺(tái)設(shè)計(jì)組</td></tr>
<tr class="child_row_01"><td>張山</td><td>男</td><td>湖北</td></tr>
<tr class="child_row_01"><td>李山</td><td>男</td><td>湖北</td></tr>
<tr class="child_row_01"><td>張山</td><td>男</td><td>湖北</td></tr>
<tr class="child_row_01"><td>張山</td><td>男</td><td>湖北</td></tr>
<tr class="parent" id="row_02"><td>前臺(tái)設(shè)計(jì)組</td></tr>
<tr class="child_row_02"><td>張山</td><td>男</td><td>湖北</td></tr>
<tr class="child_row_02"><td>張山</td><td>男</td><td>湖北</td></tr>
<tr class="child_row_02"><td>張山</td><td>男</td><td>湖北</td></tr>
<tr class="child_row_02"><td>張山</td><td>男</td><td>湖北</td></tr>
<tr class="parent" id="row_03"><td>前臺(tái)設(shè)計(jì)組</td></tr>
<tr class="child_row_03"><td>張山</td><td>男</td><td>湖北</td></tr>
<tr class="child_row_03"><td>張山</td><td>男</td><td>湖北</td></tr>
<tr class="child_row_03"><td>張山</td><td>男</td><td>湖北</td></tr>
<tr class="child_row_03"><td>張山</td><td>男</td><td>湖北</td></tr>
</tbody>
</table>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對(duì)大家學(xué)習(xí)jquery程序設(shè)計(jì)有所幫助。