1
我有一個文本輸入框jQuery的:如何隱藏取決於文本輸入表
<input type="text" id="search" />
和多個HTML桌這樣的:
<table id="table1">
<thead>
<tr>
<th>Table1 title</th>
</tr>
<tr>
<th>Name</th>
<th>Country</th>
</tr>
</thead>
<tbody>
<tr>
<td>Andrew</td>
<td>USA</td>
</tr>
<tr>
<td>John</td>
<td>Canada</td>
</tr>
</tbody>
</table>
我想這取決於數據篩選用戶在輸入框中鍵入的內容。現在
我的JS代碼:
$(document).ready(function(){
$("#search").keyup(function(){
// When value of the input is not blank
if($(this).val() != "")
{
// Show only matching TR, hide rest of them
$("#table1 > tbody > tr").hide();
$("#table1 td:contains-ci('" + $(this).val() + "')").parent("tr").show();
} else {
// When there is no input or clean again, show everything back
$("#table1 tbody > tr").show();
}
});
});
// jQuery expression for case-insensitive filter
$.extend($.expr[":"],
{
"contains-ci": function(elem, i, match, array)
{
return (elem.textContent || elem.innerText || $(elem).text() || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
}
});
一切正常確定。
但是現在我想隱藏包含thead TR的整個表格,如果輸入的文本不在表格中。
我該怎麼做?
謝謝。很棒! – blackst0ne 2013-04-21 06:41:33
但是如果我有多個桌子呢? – blackst0ne 2013-04-21 06:41:53
超過1張桌子?我沒有得到那個。你能解釋一下這個場景嗎? – PSL 2013-04-21 06:43:37