简体   繁体   English

使搜索文本框输入值不区分大小写

[英]Make search textbox input value case-insensitive

So I have this table with several rows and the following code for a search textbox: 因此,我的这张表有几行,下面是搜索文本框的代码:

<input type="text" class="textbox" id="searchTerm" onkeyup="doSearch()" placeholder="Search Rares"/>

When a user enters text into the textbox, the following script is run. 当用户在文本框中输入文本时,将运行以下脚本。 The script hides the table's rows according to the textbox input. 该脚本根据文本框输入隐藏表的行。

// JavaScript Document

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';
        }
    }

The problem is the input is not case-insensitive, so if a user types 'hello' in the textbox and there is 'Hello' in the table, the script will hide the table's row. 问题在于输入内容不区分大小写,因此,如果用户在文本框中键入“ hello”,并且表中存在“ Hello”,则脚本将隐藏表的行。

I'm a noob at Javascript, so if anyone could help me fix it up easily it would be really appreciated. 我是Java语言的菜鸟,所以如果有人可以帮助我轻松修复它,将不胜感激。

Before validating your search results, convert the input to upper case letters and then, when comparing it to the rowData, make it upper case as well. 在验证搜索结果之前,请将输入转换为大写字母,然后将其与rowData进行比较时,也将其转换为大写。

    // JavaScript Document

    function doSearch() {
    //Get the input value as lower case
    var searchText = document.getElementById('searchTerm').value.toUpperCase(); 
    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 (upper case) is not found in row data
        //then hide the row, else show
        if (rowData.toUpperCase().indexOf(searchText) == -1)
            targetTable.rows.item(rowIndex).style.display = 'none';
        else
            targetTable.rows.item(rowIndex).style.display = 'table-row';
    }
}

EDIT: 编辑:

As pointed out in the comments by Jakar, you are better off converting all text to upper case instead of lower case (which I initially recommended) The problem is that some characters might not be converted correctly. 正如Jakar的评论所指出的,最好将所有文本都转换为大写而不是小写(我最初建议这样做)。问题是某些字符可能无法正确转换。 Source: http://msdn.microsoft.com/en-us/library/bb386042.aspx 来源: http//msdn.microsoft.com/en-us/library/bb386042.aspx

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM