我一直在爲我的數據表上的Live Search開發解決方案。表 - 使用jQuery的Live Search搜索
當我搜索Jim
它按預期工作:-)
然而,當我搜索Carey
,沒有結果出現。爲什麼是這樣? :-(
演示:http://jsfiddle.net/L1d7naem/
$("#search").on("keyup", function() {
var value = $(this).val();
$("table tr").each(function(index) {
if (index !== 0) {
$row = $(this);
var id = $row.find("td:first").text();
if (id.indexOf(value) !== 0) {
$row.hide();
}
else {
$row.show();
}
}
});
});
table, tr, td, th{
border: 1px solid blue;
padding: 2px;
}
table th{
background-color: #999999;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr><th>Forename</th><th>Surname</th><th>Extension</th></tr>
<tr><td>Jim</td><td>Carey</td><td>1945</td></tr>
<tr><td>Michael</td><td>Johnson</td><td>1946</td></tr>
</table>
<br />
<input type="text" id="search" placeholder=" live search"></input>
有一個插件來做到這一點,它也處理分頁和排序也:HTTPS: //datatables.net/,但要回答你的問題,你只是比較第一個td:'$ row.find(「td:first」)。text()' – Pete
@Pete非常感謝。我正在研究這個插件。如何解決我當前的代碼來搜索其他TD? :) – michaelmcgurk
我會這樣做:http://jsfiddle.net/L1d7naem/14/ – Pete