不区分大小写的jQuery搜索过滤器

时间:2017-02-01 16:16:45

标签: javascript jquery

我在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”,它就找不到它。如何使此脚本不区分大小写?

1 个答案:

答案 0 :(得分:3)

替换它:

$(".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();
相关问题