我如何在多个表中搜索?

时间:2013-02-13 11:17:41

标签: javascript html

如何在多个表中搜索?我有这个代码,但这只适用于我的一个表。我总共有4张桌子。

这是我的表格中“某事”之后的搜索代码。

$(document).ready(function() {
    $('#search').keyup(function() {
        searchTable($(this).val());
    });
});

function searchTable(inputVal) {
    var table = $('#searchTable');
    table.find('tr').each(function(index, row) {
        var allCells = $(row).find('td');
        if(allCells.length > 0) {
            var found = false;
            allCells.each(function(index, td) {
                var regExp = new RegExp(inputVal, 'i');
                if(regExp.test($(td).text())) {
                    found = true;
                    return false;
                }
            });
            if(found == true) $(row).show();
            else $(row).hide();
        }
    });
}

我已经剥离了我的表格代码,以便它看起来更容易管理

问题在于“搜索tabe”只运行表1而不是剩余的

表1:

<table class="table" id="searchTable">

表2:

<table class="table" id="searchTable">

表3:

<table class="table" id="searchTable">

表4:

<table class="table" id="searchTable">

1 个答案:

答案 0 :(得分:6)

您的问题是由于ID。 ID必须对每个元素都是唯一的,所以一旦jQuery拉出第一个ID,它就会停止查找其他任何元素。

$('#searchTable')替换为$('.table'),问题应该解决。

如果不使用这些表,您还应该为这些表提供唯一ID或完全删除ID。

从HTML规范:“文档中不得有多个具有相同id值的元素。” http://www.w3.org/TR/html-markup/global-attributes.html

相关问题