表 - 使用jQuery进行实时搜索

mic*_*urk 2 html javascript css jquery

我一直在为我的数据表上的实时搜索解决方案.

当我搜索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();
            }
        }
    });
});
Run Code Online (Sandbox Code Playgroud)
table, tr, td, th{
    border: 1px solid blue;
    padding: 2px;
}

table th{
    background-color: #999999;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

May*_*eyz 6

因为以下几行:

var id = $row.find("td:first").text();  
Run Code Online (Sandbox Code Playgroud)

您正在处理表的第一列,而"Carey"位于表的第二列