使搜索不区分大小写
Make search case insensitive
在下面的JSFiddle链接中,我有一个国家选择过滤器的项目。
在搜索栏中,您可以在过滤器中填写您想要的国家/地区。然而,只有在使用小写字母时,它才有效。我试着编辑我的css,将文本转换为小写,但无论何时使用大写,它仍然不起作用。所以我觉得我得调整一下剧本。
但是,我不知道如何使这个脚本不区分大小写。你能帮我吗?https://jsfiddle.net/Sentah/d8so22xj/
$("#countryName").focus(function()
{
if ($(this).val() === $(this)[0].title)
{
$(this).removeClass("default_text_active");
$(this).val("");
}
});
$("#countryName").blur(function()
{
if ($(this).val() === "")
{
$(this).addClass("default_text_active");
$(this).val($(this)[0].title);
}
});
$("#countryName").blur();
$('#countryName').on('keyup', function() {
var query = this.value;
$('[id^="chk_country"]').each(function(i, elem) {
if (elem.value.indexOf(query) != -1) {
elem.style.display = 'inline';
$("#lbl_for_" + elem.getAttribute("id")).removeClass("hidden") ;
}else{
elem.style.display = 'none';
$("#lbl_for_" + elem.getAttribute("id")).addClass("hidden");
}
});
});
使用下面的代码使用query.toLowerCase()将中的文本转换为LowerCase。
$('#countryName').on('keyup', function() {
var query = this.value;
$('[id^="chk_country"]').each(function(i, elem) {
if (elem.value.indexOf(query.toLowerCase()) != -1) {
elem.style.display = 'inline';
$("#lbl_for_" + elem.getAttribute("id")).removeClass("hidden") ;
}else{
elem.style.display = 'none';
$("#lbl_for_" + elem.getAttribute("id")).addClass("hidden");
}
});
});
keyup
处理程序块中的elem.value
都是小写的。因此,为了使匹配不区分大小写,您还需要使用query.toLowerCase()
转换用户键入的小写值。试试这个:
$('#countryName').on('keyup', function () {
var query = this.value;
$('[id^="chk_country"]').each(function (i, elem) {
if (elem.value.indexOf(query.toLowerCase()) != -1) { // note toLowerCase here
elem.style.display = 'inline';
$("#lbl_for_" + elem.getAttribute("id")).removeClass("hidden");
} else {
elem.style.display = 'none';
$("#lbl_for_" + elem.getAttribute("id")).addClass("hidden");
}
});
});
相关文章:
- JavaScript Regex(?i)选项中字符串大小写不敏感的部分不起作用
- 如何使后缀函数不区分大小写
- 使搜索不区分大小写
- Regex:匹配以“AM”结尾的单词(不区分大小写比较)
- 如何使用jQuery进行不区分大小写的搜索
- 日食添加文件而不将其复制到工作区中
- 正则表达式匹配整个字符串,但大小写不敏感
- 拖放区中可拖动的 Jqueryui 不会滚动,会恢复,滚动条存在
- 将字符串添加到不区分大小写的 RegExp 中替换的事物开始
- 存储区不在网格面板内部加载-ExtJS
- javascript从字符串创建不区分大小写的regex
- :包含不区分大小写的解决方案在替换文本以突出显示时不起作用
- 当涉及到if语句时,我如何使Javascript不区分大小写
- 使用简单的角度滤波器来替换输入字符串中某些字符串的所有出现,而不考虑大小写和空格
- Regex以匹配整数或单词“”;“其他”;,不区分大小写
- Javascript中不区分大小写的自动完成
- 使.search()对变量不区分大小写
- 如果一个值在一个数组中,如果大小写不在't匹配,请将其替换为新值
- JSON Jquery大小写不敏感匹配
- 简单的大小写不敏感的搜索框的一个表与jQuery