繁体   English   中英

如何向图片库搜索框 jquery 添加不区分大小写

[英]How to add case insensitive to photo gallery search box jquery

伙计们,我是“新手”。 我有一个小任务。 我必须为“照片库”创建一个搜索框。 我是通过在 HTML 列表项中使用“数据关键字”来做到的,我还让它响应小写和大写,但问题是我想不区分大小写,但不知道该怎么做。 有一些关于 ":contains" 选择器和 RegEx 的建议,但要么我无法实现这些建议,要么我完全搞砸了代码。 这是代码https://jsfiddle.net/Lnvaagd3/在此先感谢您:)!

$("#search").keyup(function() {

    var currentQuery = $("#search").val();

    if (currentQuery !== "") {

        $("#list li").fadeOut(100);

        $("#list li").each(function() {

            var currentKeyword = $(this).attr("data-keywords");

            if (currentKeyword.toLowerCase().indexOf(currentQuery) >= 0) {

                $(this).fadeIn(200);

            } else if (currentKeyword.toUpperCase().indexOf(currentQuery) >= 0) {

                $(this).fadeIn(200);
            }

        });

    } else {

        $("#list li").fadeIn(200);

    }

});

尝试使用:

var currentQuery = $("#search").val().toLowerCase();
// ----------------------------------^^^^^^^^^^^^^^

使用它,您将不需要else if

这样currentQuerycurrentKeyword都将是小写字母,换句话说,这一行:

if (currentKeyword.toLowerCase().indexOf(currentQuery) >= 0) {

将不区分大小写。

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM