簡體   English   中英

具有多個單詞的實時搜索過濾器,使用AND代替OR

[英]Live Search Filter with Multiple Words, Use AND instead of OR

我有一個實時搜索自動填充過濾器,用於清除人員目錄。 通過這種搜索,我希望人們能夠使用多個單詞進行搜索,這將進一步減少他們獲得的結果數量。

當前,使用下面的代碼,如果有人搜索“ technical Atlanta”,他們將獲得包含“ technical”的目錄配置文件div和包含“ Atlanta”的目錄配置文件div。 我想要的是找到一個包含這兩個詞的div ...以便他們可以找到位於亞特蘭大的技術人員。 希望這是有道理的。

我的代碼致力於單獨包含這些術語,但是我希望他們查找包含多個術語的行。 有任何想法嗎? 提前致謝!!

 $("#filter").keyup(function () { // Split the current value of the filter textbox var data = this.value.split(" "); // Get the table rows var rows = $(".directoryprofile"); if (this.value == "") { rows.show(); return; } // Hide all the rows initially rows.hide(); // Filter the rows; check each term in data rows.filter(function (i, v) { for (var d = 0; d < data.length; ++d) { if ($(this).is(":contains('" + data[d] + "')")) { return true; } } return false; }) // Show the rows that match. .show(); }); 

 rows.filter(function(i, v) { var truth = true; for (var d = 0; d < data.length; ++d) { //remain true so long as all of the filters are found //if even one is not found, it will stay false truth = truth && $(this).is(":contains('" + data[d] + "')"); } return truth; }) //OR you could just flip your logic and return false if any of the //filters are not found rows.filter(function(i, v) { for (var d = 0; d < data.length; ++d) { if (!$(this).is(":contains('" + data[d] + "')")) { return false; } } return true; }) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM