JQuery搜索栏隐藏表格行



希望有人能帮助我。我有一个动态创建的表充满了信息,我试图使用JQuery只显示匹配的行正在搜索栏中键入。匹配的是每行中的第一个td。我不确定为什么这不起作用。

JQuery

var $foo = $('#table-name tr input[name="someValue"]');
$('#search').keyup(function() {
var bar = $.trim($(this).val()).toLowerCase();

$foo.parent().parent().show().filter(function() {
    var thud = $(this).val().toLowerCase();
    return !~thud.indexOf(bar);
}).hide();

当在搜索栏中输入任何字符时,无论是否有匹配,所有行都会消失。表的结构如下:

    <table id="table-name">
            <thead>
                <tr>
                    <th>0</th>
                    <th>1</th>
                </tr>
            </thead>
    <c:forEach>
    <form:form id = “{id}”>
                    <tr id="${id2}">
    <td><input type="text" class= “someClass" name= “someValue"> </input></td>
        <td><select class="someClass" name="otherValue"> </select> </td>
    </tr>
    </form:form>
</c:forEach>
</table>

为什么我的JQuery行为不正确?

没有必要重新发明轮子。试试这个:

JQuery快速搜索:http://deuxhuithuit.github.io/quicksearch/r/examples/

你的JS代码变成不超过:

$('#id_search').quicksearch('table#table_example tbody tr');

参见运行示例:http://jsfiddle.net/ddan/tqhxqwrh/1

最新更新