Jquery搜索 - 不区分大小写 [英] Jquery Search - Case insensitive
本文介绍了Jquery搜索 - 不区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Jquery脚本上有一些帮助,它创建了一个可搜索,可切换的常见问题解答。代码可以在这里看到:
I have had some help on a Jquery script which creates a searchable, toggleable FAQ. The code can be seen here:
http:// jsfiddle.net/pT6dB/62/
麻烦的是,如果大写字母H有How这个词并且我搜索 h,它不会找到它。如何使此脚本不区分大小写?
The trouble is, if there is the word "How" with an upper case "H" and I search "h", it wont find it. How can I make this script case insensitive?
推荐答案
更新
或者,您可以使用正则表达式显着减少代码量。 jsFiddle demo
$('#search').keyup(function(e) {
// create the regular expression
var regEx = new RegExp($.map($(this).val().trim().split(' '), function(v) {
return '(?=.*?' + v + ')';
}).join(''), 'i');
// select all list items, hide and filter by the regex then show
$('#result li').hide().filter(function() {
return regEx.exec($(this).text());
}).show();
});
原始
根据您当前用于确定相关元素的算法,您可以使用jQuery filter
根据关键字
数组过滤结果的方法。这是一个粗略的想法:
Original
Based on your current algorithm for determining relative elements, you could use the jQuery filter
method to filter your results based on the keywords
array. Here's a rough idea:
// select the keywords as an array of lower case strings
var keywords = $(this).val().trim().toLowerCase().split(' ');
// select all list items, hide and filter then show
$('#result li').hide().filter(function() {
// get the lower case text for the list element
var text = $(this).text().toLowerCase();
// determine if any keyword matches, return true on first success
for (var i = 0; i < keywords.length; i++) {
if (text.indexOf(keywords[i]) >= 0) {
return true;
}
}
}).show();
这篇关于Jquery搜索 - 不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文