【问题标题】:Search an HTML Table ignoring capital letters搜索忽略大写字母的 HTML 表格
【发布时间】:2013-10-29 12:13:22
【问题描述】:

我使用 php 从 .csv 生成一个 html 表。

我得到了这个运行良好的 JavaScript Livesearch,我用它来搜索表格。

这是代码:

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

    function capitaliseFirstLetter(string)
    {
        return string.charAt(0).toUpperCase() + string.slice(1);
    }
}

搜索对大写字母区分大小写,我希望它忽略这一点,以便它无论如何都会找到条目。

如果我搜索“jon doe”,我想找到:“jon doe”、“Jon doe”、“Jon Doe”、“JON DOE”等。

如何在我现有的代码中实现这一点?

【问题讨论】:

  • 您是否尝试过将大写与大写进行比较,例如当您找到要查找的字符串时,例如 Jon Doe,将其转换为 JON DOE,然后与您要比较的字符串进行比较, 也将其转换为 JON DOE,这样无论表中的内容或您要搜索的内容,您都只会比较所有大写字母。
  • 这对我来说似乎是个好主意,不过我对 JS 很陌生,你能告诉我你将如何在代码中做到这一点吗?

标签: javascript search html-table livesearch


【解决方案1】:

你可以用 ToUpperCase 来比较字符串 例如:

var myName = 'Jon Doe';

然后你会为你要检查的任何名字做同样的事情

var searchName = GetElementById("MyTextbox");

var areEqual = myName.toUpperCase() === searchName.toUpperCase();

那你就可以了

if(areEqual == True) {
    document.write("Names Match");
}

【讨论】:

  • 谢谢,这不是我需要的 100%,但它让我直奔正确的方向 :)
  • 我对 JavaScript 了解不多,所以我不知道如何将它添加到您现有的代码中,但很高兴我以某种方式提供了帮助 :)
猜你喜欢
  • 1970-01-01
  • 2021-01-29
  • 2016-06-06
  • 2013-04-11
  • 2011-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-20
相关资源
最近更新 更多