我有一个非常适合我的应用程序的搜索功能,只是我不知道如何使其不区分大小写。下面是一些 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/