简体   繁体   English

如何使用 jquery 从过滤列表中获取可见项目的数量?

[英]How can i get the number of visible items from a filtered list using jquery?

I have a list of 4 divs and an input field for filtering the list using jquery.我有一个包含 4 个 div 的列表和一个用于使用 jquery 过滤列表的输入字段。 What i need is to display/count the number of the filtered results on "keyup" function .我需要的是在“keyup”功能上显示/计算过滤结果的数量。 I am using the "size" function to get the total number of the results.我正在使用“大小”函数来获取结果的总数。

However i 'm not getting the right number of results and i can't fix it.但是我没有得到正确数量的结果,我无法修复它。

Here's my code:这是我的代码:

 var langMap = {} $('#search-stores-box').keyup(function(){ var valThis = $(this).val().toLowerCase(); var filteredWord = getLatinWord(valThis); var count = $('.storesList .store-block').size() - $('.storesList .hidden-store').size(); $('#count').text(count); if(filteredWord == ""){ $('.storesList .store-block').show(); $('.storesList .store-block').removeClass('hidden-store'); } else { $('.storesList .store-block').each(function(){ $('.storesList .store-block').addClass('hidden-store'); var text = $(this).text().toLowerCase(); text = getLatinWord(text); (text.indexOf(filteredWord) > -1 ) ? $(this).show() : $(this).hide(); }); } }); function getLatinWord(word){ return word.split('').map(function(character){ if (langMap[character]) { return langMap[character]; } return character; }).join(''); }
 .results-box { margin-bottom:10px; overflow:hidden; display:inline-block; } .search-area {margin-bottom:10px;} #count {display:inline-block;} .store-block { width:80%; margin-bottom:10px; padding:5px; background:#e5e5e5; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="results-box">Number of stores: <div id="count"></div> </div> <div class="search-area"> <input placeholder="Type a store name..." id="search-stores-box" type="text" /> </div> <div class="storesList"> <div class="store-block">Apple Store</div> <div class="store-block">Microsoft Store</div> <div class="store-block">Motorola Store</div> <div class="store-block">Nokia Store</div> </div>

JSFIDDLE HERE JSFIDDLE 在这里

you can get count using :visible您可以使用:visible计数

var count = $('.storesList  .store-block:visible').length;
$('#count').text(count);

OR you can check store-blobk with no hidden-store class或者你可以检查 store-blobk 没有 hidden-store 类

var count = $('.storesList  .store-block:not(.hidden-store)').length;
$('#count').text(count);

check below working code snippet检查下面的工作代码片段

 var langMap = {} $('#count').text($('.storesList .store-block:visible').length); $('#search-stores-box').keyup(function(){ var valThis = $(this).val().toLowerCase(); var filteredWord = getLatinWord(valThis); if(filteredWord == ""){ $('.storesList .store-block').show(); $('.storesList .store-block').removeClass('hidden-store'); } else { $('.storesList .store-block').each(function(){ $('.storesList .store-block').addClass('hidden-store'); var text = $(this).text().toLowerCase(); text = getLatinWord(text); (text.indexOf(filteredWord) > -1 ) ? $(this).show() : $(this).hide(); }); } var count = $('.storesList .store-block:visible').length; $('#count').text(count); }); function getLatinWord(word){ return word.split('').map(function(character){ if (langMap[character]) { return langMap[character]; } return character; }).join(''); }
 .results-box { margin-bottom:10px; overflow:hidden; display:inline-block; } .search-area {margin-bottom:10px;} #count {display:inline-block;} .store-block { width:80%; margin-bottom:10px; padding:5px; background:#e5e5e5; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="results-box">Number of stores: <div id="count"></div> </div> <div class="search-area"> <input placeholder="Type a store name..." id="search-stores-box" type="text" /> </div> <div class="storesList"> <div class="store-block">Apple Store</div> <div class="store-block">Microsoft Store</div> <div class="store-block">Motorola Store</div> <div class="store-block">Nokia Store</div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取过滤后的数组项的索引 - How can I get the indexes of a filtered array items 如何使用IE和FF使jQuery以编程方式在此选择列表中的项目保持一致? - How can I get jQuery to programmatically items in this select list using IE and FF consistently? 如何使用jQuery从列表(没有商品ID)中获取值? - How can I get values from a list (not having items ids) with jQuery? 如何从 webpack 5 中的编译中获取按入口点过滤的文件依赖项的平面列表 - How can i get a flat list of file dependencies filtered by entry point from the compilation in webpack 5 如何从jquery过滤的项目中删除空格? - How can I remove spaces from a jquery filtered item? 使用jQuery,如何清除画布上的项目? - Using jquery, how can I clear items from a canvas? 如何使用控制台从下拉列表中 select 项目? - How can I select items from a dropdown list using the console? 我如何从每个ID获取数字并使用jquery附加到另一个数据属性? - How can I get number from each id and append to another data attribute using jquery? 如何使用jQuery获取每个元素的索引号 - How can I get the index number of every element using jQuery 如何 select 显示特定数量的过滤项目? - How do I select a specific number of filtered items to display?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM