javascript - 搜索表javascript

标签 javascript html

如何搜索列 random ID在表中,我只能搜索 unique ID 中的值柱子。似乎是什么错误?

示例代码:

<table>
        <tr><th>Unique ID</th><th>Random ID</th></tr>
        <tr><td>214215</td><td>442</td></tr>
        <tr><td>1252512</td><td>556</td></tr>
        <tr><td>2114</td><td>4666</td></tr>
        <tr><td>3245466</td><td>334</td></tr>
        <tr><td>24111</td><td>54364</td></tr>
    </table>
    <br />
    <input type="text" id="search" placeholder="  live search"></input>


$("#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();
            }
        }
    });
});

最佳答案

您需要使用 :nth-child(2) 而不是 :first 来获取第二个 td 而不是第一个:

更多信息 here

$("#search").on("keyup", function() {
    var value = $(this).val();

    $("table tr").each(function(index) {
        if (index !== 0) {

            $row = $(this);

            var id = $row.find("td:nth-child(2)").text();

            if (id.indexOf(value) !== 0) {
                $row.hide();
            }
            else {
                $row.show();
            }
        }
    });
});

关于javascript - 搜索表javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47625873/

相关文章:

javascript - 使用 JavaScript/ajax 在单击按钮上更改特定 TD 的背景颜色

javascript - 是否可以关闭窗口/选项卡?

javascript - 如何在严格模式下找到函数调用者

html - 将文本用于链接,将 XSL 用于 HTML 弹出窗口

javascript - 左边的侧边栏需要两侧

javascript - 使用 JavaScript 或 jQuery 验证输入字段

javascript - 如何使用 jquery 在运行时创建 json 对象数组?

html - 将内联 svg 保存为图像文件

javascript - 删除动态生成按钮的 onclick 内容

html - Osclass 使内容与类别并排