简体   繁体   English

如何使我的搜索表单使用大写和小写

[英]How can I make my search form work with uppercase and lowercase

I want my search form to work with uppercase and lowercase. 我希望我的搜索表单可以使用大写和小写字母。 So whenever I input something that's lowercase it will also show me te uppercase results in the table I'm searching in. 因此,每当我输入小写字母时,它也会在要搜索的表中向我显示大写结果。

Here's my javascript 这是我的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';
}

Can anyone help me with this? 谁能帮我这个?

Change this line: 更改此行:

 if (rowData.indexOf(searchText) == -1)

to: 至:

 if (rowData.toLowerCase().indexOf(searchText.toLowerCase()) == -1)

This way, for comparison, you will be using lower case strings only. 这样,为了进行比较,您将仅使用小写字符串。 Ie if searchText was "Lorem Ipsum" and rowData was "I HAVE LOREM IPSUM WITHIN", they'll become "lorem ipsum" and "i have lorem ipsum within" respectively - thus, they'll match. 即,如果searchText是“ Lorem ipum”,而rowData是“ I LOVE IPSUM WITHIN”,它们将分别变为“ lorem ipsum”和“我内部有lorem ipsum”-因此,它们将匹配。

String.prototype.toLowerCase() reference. String.prototype.toLowerCase()引用。

Also note that in ES6 you have String.prototype.includes() that might be used for what you're doing. 还要注意,在ES6中,您可以使用String.prototype.includes()进行操作。 It'll read a bit better - however it's not supported in IE at the moment. 它会读得更好-但是IE目前不支持它。

If you want to ignore case, may be you should make both side to be upper case or lower case. 如果您想忽略大小写,可能您应该将双方设为大写或小写。

In your case, you can use toUpperCase() or toLowerCase() on rowData and searchText . 就您而言,可以在rowDatasearchText上使用toUpperCase()toLowerCase()

var upperData = rowData.toUppercase();
var upperText = searchText.toUppercase();
if (upperData.indexOf(upperText) == -1) {
  //Not Found
}
else {
  //Found
}

This is an alternative: 这是一种替代方法:

The trick is homogenize the comparison. 诀窍是使比较均匀。

How to do this homogenization? 如何进行均质化?

  • Just convert the elements to compare to the same type. 只需将元素转换为相同类型即可。
  • But they are strings, yeah but A is different to a . 但是它们是字符串,是的,但是Aa不同。 So, we may lowercase all or uppercase all. 因此,我们可以全部小写或全部大写。 But, How? 但是,如何?
  • Using toLowerCase() or toUpperCase()() methods. 使用toLowerCase()toUpperCase()()方法。

I created a notFound() that lowercase rowData and searchText and then returns false if the indexOf is -1 otherwise will return true . 我创建了一个notFound()小写的rowDatasearchText ,然后如果indexOf为-1则返回false ,否则将返回true Sorry, It took me long time to fill the table with countries and continents. 抱歉,我花了很长时间才把国家和大洲塞满了桌子。

 var searchButton = document.getElementById('search_button'); searchButton.addEventListener('click', searchFunction) function searchFunction() { var searchText = document.getElementById('database_search_bar').value; var targetTable = document.getElementById('database_table'); var notFoundText = document.getElementById('not_found'); var targetTableColCount; var hasResult = false; notFoundText.innerHTML = ''; 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 (notFound(rowData, searchText)) { targetTable.rows.item(rowIndex).style.display = 'none'; } else { targetTable.rows.item(rowIndex).style.display = 'table-row'; hasResult = true; } } if (!hasResult){ notFoundText.innerHTML = 'There are no results.'; } } function notFound(rowData, searchText){ return rowData.toLowerCase().indexOf(searchText.toLowerCase()) === -1; } 
 table td { width: 150px; } 
 Search Text: <input id="database_search_bar" type="text" name="fname"><input id="search_button" type="submit" value="Search"> <p></p> <table id="database_table" > <tr> <th>Country</th> <th>Continent</th> </tr> <tr> <td>Brazil</td> <td>South America</td> </tr> <tr> <td>Jordan</td> <td>Asia</td> </tr> <tr> <td>Mauritania</td> <td>Africa</td> </tr> <tr> <td>United States of America</td> <td>North America</td> </tr> <tr> <td>Netherlands</td> <td>Europe</td> </tr> <tr> <td>Oman</td> <td>Asia</td> </tr> <tr> <td>Honduras</td> <td>South America</td> </tr> <tr> <td>Lithuania</td> <td>Europe</td> </tr> </table> <div id="not_found" ></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使我的前缀适用于小写和大写? - how to make my prefix works on lowercase and uppercase? 如何使用 javascript 制作密码生成器,我可以在其中切换长度、特殊字符、数字、大写和小写 - How to make a password generator with javascript where i can toggle length, special characters, numbers, uppercase and lowercase 如何使用 JavaScript 测试字符串中的字母是大写还是小写? - How can I test if a letter in a string is uppercase or lowercase using JavaScript? 大写和小写搜索 - Uppercase and lowercase search 如何在字符串upperCase或lowerCase中制作特定单词? - How to make specific word in string upperCase or lowerCase? 如何使查询忽略小写和大写? - How to make Query ignores lowercase and uppercase? 无法弄清楚为什么我的 ROT13 转换器可以使用小写字母但不能使用大写字母 - Can't figure out why my ROT13 converter works with lowercase but it doesn't work with uppercase 如何让我的 javascript 表单重置按钮工作? - How can I make my javascript form reset button work? 如何阻止我的搜索功能检查大小写(大写/小写)-Javascript / ReactJS - How to stop my search function from checking cases (Uppercase/lowercase) - Javascript/ReactJS 我如何让我的正则表达式用附加变量将小写字母替换为大写字母 - How do i get my regex to replace lowercase to uppercase with additional variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM