搜索选项在较低和较高的情况下不起作用

时间:2014-02-23 10:24:21

标签: javascript jquery html

我在我的jsfiddle中创建了一个搜索选项:http://jsfiddle.net/alonshmiel/46wyH/33/

所以我有一个值(value是搜索到的文本。)

我搜索包含值的所有li:value

// hide all the lists
$('#searchExcludeResult li').each(function() {       
      $(this)[0].style.display = "none";
});

// show all the li that contains the value: value
$('#searchExcludeResult li:contains(' + value + ')').each(function() {       
    $(this)[0].style.display = "list-item";
    $(this)[0].innerHTML = $(this)[0].textContent.replace(value, 
                                '<span style="font-weight: bold">' + value + '</span>');
});

它有效,但无论搜索的文本是小写还是大写,我都希望它能够正常工作。

例如:

<ul>
    <li>United States</li>
    <li>London, England</li>
    <li>Moskow, Russia</li>
</ul>

,搜索到的文字为:e,显示第一个(单位 e d stat e s)和第二个li(伦敦, E ngland)。

搜索到的文字可以是ENeNlL等。

任何帮助表示赞赏!

2 个答案:

答案 0 :(得分:2)

Fiddle Demo

使用.filter()并匹配.toLowerCase() .text()

$('#searchExcludeResult li').filter(function () {
    return $(this).text().toLowerCase().indexOf(value.toLowerCase()) !== -1;
}).each(function () {

答案 1 :(得分:1)

您可以将.contains过滤器更改为不区分大小写,也可以创建自己的选择器。

jQuery.expr[':'].contains = function(a, i, m) {
 return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0;
};

DEMO

相关问题