gpt4 book ai didi

javascript - 使用输入文本框搜索和过滤 html 表数据

转载 作者:行者123 更新时间:2023-11-28 20:12:37 24 4
gpt4 key购买 nike

我有一个使用 php 动态填充的表。我想为其添加搜索功能。在 stackoverflow 上搜索类似问题后,我发现了一个我尝试过的 JS 片段。

var $rows = $('#existing tr');
$('#search').keyup(function() {
var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();

$rows.show().filter(function() {
var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
return !~text.indexOf(val);
}).hide();
});

jsfiddle链接:http://jsfiddle.net/kvkBw/3/

问题是,当我输入任何搜索词时,表格本身会被隐藏(变得不可见)任何帮助将不胜感激,谢谢!。

请注意,php 代码已被删除,因为 jsfiddle 不支持 php,同时也是为了增加可读性

最佳答案

首先,你的函数搜索是错误的,什么是 !~ ,以及为什么你试图隐藏你发现的所有事件?

试试这个:

var $rows = $('#existing tbody tr:not(:first)'); // this is the reason for table                        hidding like @drizzie says

$('#search').keyup(function () {
var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();

$rows.hide().filter(function () {
var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();

return text.indexOf(val) != -1 ;
}).show();
});

但最好看看 DEMO

关于javascript - 使用输入文本框搜索和过滤 html 表数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19667922/

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