【问题标题】:Case insensitive jQuery search filter不区分大小写的 jQuery 搜索过滤器
【发布时间】:2017-02-01 16:16:44
【问题描述】:

我在创建可搜索过滤器的 Jquery 脚本上得到了一些帮助,代码可以在这里看到:

$('#search-keyword').on( "keyup", function(){
    if($(this).val()){
        var input = $(this).val();
        $(".filter").hide();
        $("div[data-destination*='"+ input +"']").show();
        if(!$('.filter:visible').get(0)){
            $(".filter").show();
        }
    }else{
        $(".filter").show();
    }
});

问题是,如果有一个大写“H”的单词“How”,我搜索“h”,它不会找到它。如何使此脚本不区分大小写?

【问题讨论】:

  • 这个 html 是做什么用的?您想将数据属性更改为 [data-destination*='/"+input+"/gi'] 我认为。
  • 除了标记为重复的答案之外,您可以更改 html 的生成方式吗?即当你生成<div data-destination=...在它生成的时候把它变成小写。那么你只需要将输入转换为lower并进行比较即可。
  • @freedomn-m ,谢谢你的想法,你拯救了我的一天

标签: javascript jquery


【解决方案1】:

替换这个:

$(".filter").hide();
$("div[data-destination*='"+ input +"']").show();

用这个:

$(".filter div[data-destination]").hide(); // You have to hide the elements (the divs you want to filter) not the container.
$(".filter div[data-destination]").filter(function() {
    return $(this).data("destination").toLowerCase().indexOf(input.toLowerCase()) !== -1;
}.show();

【讨论】:

  • 它没用,关于'.filter',它是完整列表的类,我隐藏它只显示搜索结果
猜你喜欢
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-25
  • 1970-01-01
  • 2013-08-07
相关资源
最近更新 更多