如何进行不区分大小写的搜索?

时间:2022-10-16 20:15:55
$("#input").keyup(function () {
    var userInput = $(this).val();
    $("#list div").map(function (index, value) {
        $(value).toggle($(value).text().toString().indexOf(userInput) >= 0);
    });
});

here is the code

这是代码

How do I make it case insensitive? is it possible? I tried using css to force lowercase on user input but did not help.

如何使其不区分大小写?可能吗?我尝试使用css强制用户输入小写,但没有帮助。

3 个解决方案

#1


1  

This should do the trick

这应该可以解决问题

var userInputLower = userInput.toLowerCase();
var shouldToggle = $(value).text().toLowerCase().indexOf(userInputLower) >= 0;
$(value).toggle(shouldToggle);

#2


0  

Use string.toLowerCase()

使用string.toLowerCase()

"HELLOOOooo".toLowerCase(); // hellooooo

#3


0  

The idea behind case insensitive searches it to convert both the source (string to look for) as well as the target (strings that are compared to the source) to lowercase. Then both have the same casing, which makes it case insensitive.
Converting both the source and target strings to uppercase will give the same result, but to lowercase is used most often.

不区分大小写的背后的想法是将它转换为源(要查找的字符串)以及将目标(与源进行比较的字符串)转换为小写。然后两者都有相同的外壳,这使它不区分大小写。将源字符串和目标字符串转换为大写将产生相同的结果,但最常使用小写字母。

#1


1  

This should do the trick

这应该可以解决问题

var userInputLower = userInput.toLowerCase();
var shouldToggle = $(value).text().toLowerCase().indexOf(userInputLower) >= 0;
$(value).toggle(shouldToggle);

#2


0  

Use string.toLowerCase()

使用string.toLowerCase()

"HELLOOOooo".toLowerCase(); // hellooooo

#3


0  

The idea behind case insensitive searches it to convert both the source (string to look for) as well as the target (strings that are compared to the source) to lowercase. Then both have the same casing, which makes it case insensitive.
Converting both the source and target strings to uppercase will give the same result, but to lowercase is used most often.

不区分大小写的背后的想法是将它转换为源(要查找的字符串)以及将目标(与源进行比较的字符串)转换为小写。然后两者都有相同的外壳,这使它不区分大小写。将源字符串和目标字符串转换为大写将产生相同的结果,但最常使用小写字母。