javascript - 如何创建不区分大小写的文本搜索

标签 javascript jquery search

我有一个非常适合我的应用程序的搜索功能,只是我不知道如何使其不区分大小写。下面是一些 HTML 的示例:

 <div id='search_count'>
   <div id='count'><?php  get_count('notes', 'tasks') ?>
   </div>
    <input type="text" id='search_notes' placeholder='Search'/>
   </div>

   <div class='task'>
    301 closet  
   </div>

 <div class='notes'>
 <p>The old manager opened a panel that needs to be closed.</p>
 </div>

 <div class='task'>vacuum halls </div>

 <div class='notes'><p>The halls need to be vacuumed every week!</p></div>

这个 JavaScript 会像我想要的那样过滤带有 class='notes' 的 div,但只区分大小写。我该如何做到不区分大小写?

$("#search_notes").keyup(function () {
    var search_notes = $(this).val();
    $('.task, .notes').css('display', 'none');

    $("div.notes:contains(" + search_notes + ")")
        .css('display', 'block')
        .prev().css('display', 'block');


    var count = $('div.notes').filter(function () {
        return $(this).css('display') !== 'none';
    }).length;

    $('#count').text(count);
});

最佳答案

您必须使用 toLowerCase() 相互测试两个小写值方法:

var search_notes = $(this).val().toLowerCase();

$('.task, .notes').css('display', 'none');

$('div.notes').filter(function() {
  return $(this).text().toLowerCase() === search_notes;
}).forEach(function() {
  $(this)
    .css('display', 'block')
    .prev()
    .css('display', 'block');
});


var count = $('div.notes').filter(function() {
return $(this).css('display') !== 'none';
}).length;

$('#count').text(count);

关于javascript - 如何创建不区分大小写的文本搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50892757/

相关文章:

jQuery 显示/隐藏与其他具有相同类的单个 div

javascript - 替换 jQuery 单击事件上的标题文本

javascript - 为什么 selectedIndex 在此代码中不起作用?

javascript - 在 HIGHCHARTS 中无法设置仪表中的最大点

c# - 有没有办法将代码镜像中编写的 css 应用于 iframe 的 html 内容?

jQuery 和 Colorbox : How to automatically set the height and width of an iframe colorbox

c# - 如何在 Lucene.net 中执行语音和近似搜索

python - 如何在 tweepy 上搜索一条推文中的多个单词?

java 。 GUI WindowBuilder 通过单击按钮从 JTextField 读取

javascript - 滚动jquery css动画的飞入飞出效果