gpt4 book ai didi

javascript - 表 - 使用 jQuery 进行实时搜索

转载 作者:太空宇宙 更新时间:2023-11-04 13:37:36 24 4
gpt4 key购买 nike

我一直在研究针对我的数据表进行实时搜索的解决方案。

当我搜索 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>

最佳答案

这是因为下面一行:

var id = $row.find("td:first").text();  

您只处理表格的第一列,“Carey”在表格的第二列

关于javascript - 表 - 使用 jQuery 进行实时搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39224447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com