【发布时间】:2021-03-09 09:51:07
【问题描述】:
我想编写一个 js 函数,让我可以在表格中进行搜索。 我只想显示那些包含任何
中的搜索文本的行虽然我能够使其仅适用于特定列,但我没有成功适用于整行。所以我猜它在内部 for 循环中的某个地方。知道我哪里做错了吗?
感谢您的支持! 这是我的代码:
function myFunction() {
var input, filter, table, tr, td, i, txtValue, allText, j;
input = document.getElementById("ipt-Search"); //my input text field where I enter the search term
filter = input.value.toUpperCase();
table = document.getElementById("tbl-RS"); // the table I want to search and filter
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
allText = "";
for (j = 0; j < tr.cells.length; j++) {
td = tr[i].getElementsByTagName("td")[j];
if (td) {
allTest = all Test + " " + td.textContent || td.innerText;
}
}
if (allText.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
【问题讨论】:
标签: javascript filter html-table