我希望我的搜索表单能够使用大写和小写。因此,每当我输入小写内容时,它也会在我正在搜索的表中显示大写结果。
这是我的 JavaScript
function searchFunction() {
var searchText = document.getElementById('database_search_bar').value;
var targetTable = document.getElementById('database_table');
var targetTableColCount;
for (var rowIndex = 0; rowIndex < targetTable.rows.length; rowIndex++) {
var rowData = '';
if (rowIndex == 0) {
targetTableColCount = targetTable.rows.item(rowIndex).cells.length;
continue;
}
for (var colIndex = 0; colIndex < targetTableColCount; colIndex++) {
rowData += targetTable.rows.item(rowIndex).cells.item(colIndex).textContent;
}
if (rowData.indexOf(searchText) == -1)
targetTable.rows.item(rowIndex).style.display = 'none';
else
targetTable.rows.item(rowIndex).style.display = 'table-row';
}
谁能帮我解决这个问题吗?
最佳答案
更改此行:
if (rowData.indexOf(searchText) == -1)
至:
if (rowData.toLowerCase().indexOf(searchText.toLowerCase()) == -1)
这样,为了进行比较,您将仅使用小写字符串。 IE。如果 searchText
为“Lorem Ipsum”且 rowData
为“I HAVE LOREM IPSUM Within”,则它们将分别变为“lorem ipsum”和“i have lorem ipsum inside” -因此,它们会匹配。
String.prototype.toLowerCase()
reference.
另请注意,在 ES6 中,您有 String.prototype.includes()
这可能会用于你正在做的事情。它读起来会更好一些 - 但目前 IE 不支持它。
关于javascript - 如何使我的搜索表单适用于大写和小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43582408/