Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
搜索表javascript_Javascript_Html - Fatal编程技术网

搜索表javascript

搜索表javascript,javascript,html,Javascript,Html,如何搜索表中的列随机ID,我只能搜索唯一ID列中的值。有什么错误吗 示例代码: <table> <tr><th>Unique ID</th><th>Random ID</th></tr> <tr><td>214215</td><td>442</td></tr> <tr><td&

如何搜索表中的列
随机ID
,我只能搜索
唯一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();
            }
        }
    });
});

唯一IDRandom ID
214215442
1252512556
21144666
3245466334
2411154364

$(“#搜索”)。在(“键控”,函数()上{ var值=$(this.val(); $(“表tr”)。每个(函数(索引){ 如果(索引!==0){ $row=$(此项); var id=$row.find(“td:first”).text(); 如果(id.indexOf(值)!==0){ $row.hide(); } 否则{ $row.show(); } } }); });
您需要使用
:第n个子项(2)
而不是
:第一个
来获取第二个td,而不是第一个td:

更多信息


使用第n个子属性

$(“#搜索”)。在(“键控”,函数()上{
var值=$(this.val();
$(“表tr”)。每个(函数(索引){
如果(索引!==0){
$row=$(此项);
var id=$row.find(“td:nth-child(2)”).text();
如果(id.indexOf(值)!==0){
$row.hide();
}
否则{
$row.show();
}
}
});
});

唯一IDRandom ID
214215442
1252512556
21144666
3245466334
2411154364

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