需要帮助以使jQuery .find不区分大小写 [英] Need help to make jQuery .find case insensitive
问题描述
尝试制作使用不区分大小写.find的jQuery过滤器.
Trying to make this jQuery filter that uses .find case-insensitive.
例如,当搜索="cat"(不带引号)时,它将找到"cat"而不是"Cat"或"CAT".同样,猫"找到猫",但找不到猫".
For example, when the search = "cat" (no quotes) it will find "cat" but not "Cat" or "CAT". Similarly, "Cat" finds "Cat" but not "cat".
我怀疑这是一个非常基本的东西,但是无法弄清楚,所以我在这里发表我的第一篇文章.
I suspect it's something very basic but cannot figure it out so I'm reaching out here with my first post.
我无法控制列表项本身的内容,因此无法在其中进行任何修改,
I have no control over the contents of the list items themselves so cannot modify anything there,
提前感谢您的帮助!
PS请不要大呼小叫一些可能对你们来说很初级的东西. ;)
PS Please no catcalls for something probably pretty junior for you guys. ;)
$("#filter").live("keyup", function (ev) {
var $filter = $(this),
keyCode = ev.keyCode || ev.which,
search;
if (keyCode === 27) { //ESC
$filter.val("");
}
search = $(this).val();
$("#results").find("li").each(function () {
var $li = $(this);
if (search === "") {
$li.show();
} else {
if (_.include(service_names, search)) {
$li.toggle($li.hasClass("items-" + search));
} else {
if ($li.text().search(search) > 0) {
$li.show();
} else {
$li.hide();
}
}
}
});
});
推荐答案
您要进行不区分大小写的搜索.尝试使用正则表达式search(/string/i)
you want to make case insensitive searches. try using regex search(/string/i)
$("#filter").live("keyup", function (ev) {
var $filter = $(this),
keyCode = ev.keyCode || ev.which,
search;
if (keyCode === 27) { //ESC
$filter.val("");
}
search = $(this).val() ;
// added
var search_regex = new RegExp(search, 'i');
$("#results").find("li").each(function () {
var $li = $(this);
if (search === "") {
$li.show();
} else {
if (_.include(service_names, search)) {
$li.toggle($li.hasClass("items-" + search));
} else {
// changed here
if (search_regex.test($li.text())) {
$li.show();
} else {
$li.hide();
}
}
}
});
});
这篇关于需要帮助以使jQuery .find不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!