This question already has answers here:
jQuery :contains() selector uppercase and lower case issue
                                
                                    (4个答案)
                                
                        
                                5个月前关闭。
            
                    


$('#asearch').on('input', function(){
	let a = $(this).val();
	$('.title').hide();
	$('.title:contains(' + a + ')').show();
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type = 'search' class='asearch' id='asearch' placeholder='Search'>
<div class='title'>loRem</div>
<div class='title'>IpsuM</div>





如果要在搜索框中键入lor,则要使第一个标题可见;如果要键入i,则要显示第二个标题,即要使搜索框不区分大小写。

有什么帮助吗?

最佳答案

jQuery中的:contains选择器是case sensitive。您可以将正则表达式与filter()一起使用



$('#asearch').on('input', function() {
  let a = $(this).val();
  $('.title').hide();
  $('.title').filter(function() {
    return new RegExp(a, 'i').test($(this).text())
  }).show();
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type='search' class='asearch' id='asearch' placeholder='Search'>
<div class='title'>loRem</div>
<div class='title'>IpsuM</div>

关于javascript - 使搜索框不区分大小写,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57320995/

10-12 07:01