[英]How can i search in multiple tables?
如何在多个表格中搜索? 我有这段代码,但这仅适用于我的一张桌子。 我总共有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();
}
});
}
我已经删除了表中的代码,以使其看起来更易于管理
问题在于“搜索表”仅运行表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">
您的问题是由于ID。 ID对每个元素必须唯一,因此,一旦jQuery拉出第一个ID,它将停止寻找其他元素。
将$('#searchTable')
替换$('#searchTable')
$('.table')
,此问题应得到解决。
您还应该为这些表提供唯一的ID,或者如果不使用它们,则将其完全删除。
根据HTML规范:“文档中不得有多个具有相同id值的元素。” http://www.w3.org/TR/html-markup/global-attributes.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.