function doSearch() {
var searchText = document.getElementById('searchTerm').value;
var targetTable = document.getElementById('dataTable');
var targetTableColCount;
// Loop through table rows.
for (var rowIndex = 0; rowIndex < targetTable.rows.length; rowIndex++) {
var rowData = '';
// Get column count from header row.
if (rowIndex == 0) {
targetTableColCount = targetTable.rows.item(rowIndex).cells.length;
continue; //do not execute further code for header row.
}
// Process data rows. (rowIndex >= 1)
for (var colIndex = 0; colIndex < targetTableColCount; colIndex++) {
rowData += targetTable.rows.item(rowIndex).cells.item(colIndex).textContent;
}
// If search term is not found in row data then hide the row, else show.
if (rowData.indexOf(searchText) == -1)
targetTable.rows.item(rowIndex).style.display = 'none';
else
targetTable.rows.item(rowIndex).style.display = 'table-row';
}
}
这段代码非常适合搜索一个关键字,但我想搜索用分号 (;) 分隔的多个关键字。
最佳答案
首先让你的函数 doSearch 带有一个参数。 而不是:
function doSearch() {
var searchText = document.getElementById('searchTerm').value;
放
function doSearch(searchValue, rows) {...}
然后做
var keywords = document.getElementById('searchTerm').value.split(';');
并循环函数doSearch(keyword[index], rowIndexes);
包含所有关键字
让函数 doSearch() 返回一个具有匹配行索引的数组。 并将其传递给下一次调用,直到所有关键字完成。
最后的结果将是匹配所有关键字的行索引。
关于java - 从 HTML 表中进行多重搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18204798/