javascript - PHP 和 jQuery 实时搜索表格问题

标签 javascript jquery html html-table livesearch

我有一个 JS 文件来实时搜索表格。它工作正常,并在您搜索时在页面上显示结果。

Javascript:

function Search()
{
var $rows = $('#table tr');
$('#search').keyup(function() {

var val = '^(?=.*\\b' + $.trim($(this).val()).split(/\s+/).join('\\b)(?=.*\\b') + ').*$',
    reg = RegExp(val, 'i'),
    text;

$rows.show().filter(function() {
    text = $(this).text().replace(/\s+/g, ' ');
    return !reg.test(text);
}).hide();
});
}

HTML

 <table class="table" id="table">
    <input type="text" id="search" placeholder="Type to search" onkeyup="Search();">
    <tr>
        <th>Naam</th>
        <th>Personeelsnummer</th>
        <th>Geblokkeerd niveau</th>
        <th>Wachtwoord</th>
        <th>Verwijderen</th>
        <th>(De)blokkeren</th>
    </tr>
    <?php
     while ($row = mysqli_fetch_array($result)) {?>
    <tr>
    <td><?php echo $row['P_Naam'];?></td>
    <td><?php echo $row['P_ID'];?></td>
    <td><?php echo $row['P_Blocked'];?></td>

代码继续在表格中添加了一些按钮。

问题是,当它返回搜索时,表标题会消失。我一直试图找出问题所在,但似乎无法弄清楚。

最佳答案

当您选择

var $rows = $('#table tr');

它还选择这些 header :

<tr>
    <th>Naam</th>
    <th>Personeelsnummer</th>
    <th>Geblokkeerd niveau</th>
    <th>Wachtwoord</th>
    <th>Verwijderen</th>
    <th>(De)blokkeren</th>
</tr>

并删除它们。

尝试更改 jQuery 过滤器以跳过第一行。

关于javascript - PHP 和 jQuery 实时搜索表格问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37269191/

相关文章:

javascript - jquery .html() 注入(inject)对象与包含对象的变量

jquery - 如何创建 JQuery 导航栏?

javascript - 如何通过Jquery中的函数调用设置变量?

javascript - 如何从元素集中获取 id 并保存在数组中?

javascript - 类型错误 : Cannot read property 'maps' of undefined (Google maps places autocomplete Angular 2)

javascript - 将字符串解析为时间格式 JavaScript

javascript - 本地托管时日期选择器不起作用

jquery - overflow-y、固定元素和滚动条噩梦

java - 如何制作GWT模板?

html - 将图像置于主要内容的中心,同时具有固定的侧边栏