从 HTML 表中多次搜索
Multiple search from HTML table
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(( 返回一个包含匹配行索引的数组。并将其传递给下一个调用,直到所有关键字完成。
最后一个结果将是匹配所有关键字的行索引。
相关文章:
- 在html Select中添加搜索
- AngularJS/HTML/Bootstrap元素用于动态搜索结果
- 为非列表项目创建HTML实时搜索
- 使用php在html中搜索硬编码的JavaScript变量
- 搜索并用jQuery替换整个HTML字符串
- 使用JS和jQuery搜索HTML表
- 搜索 HTML 表并查找具有必填类的文本字段
- 谷歌搜索HTML
- 搜索HTML文本区域中给定的单词,并用JavaScript在字符串中突出显示它们
- 如何搜索HTML的每一部分
- 如何通过ng模型搜索html中的文本
- 如何搜索html标签并删除它
- 使用list.js(搜索函数)搜索HTML表-修改以显示所有不包括搜索词的行
- JavaScript搜索功能也在搜索html代码
- 在字符串中搜索HTML元素
- 使用url在网页中搜索HTML标签
- 基于数据属性搜索html元素
- 使用jQuery搜索HTML字符串
- 使用数据表在Javascript中搜索html/php表不工作
- 使用 Javascript 搜索 HTML 表并保留找到匹配项的完整表行并删除其余部分