我有一個內部div中有標籤的帖子鏈接列表。用戶從三個不同的列表中選擇來過濾帖子。jQuery邏輯過濾
基本上我想實現的是基於用戶選擇的三個列表的內容進行前端過濾。
我想要的邏輯基本上是這樣的:IF post-tags-list has 1+ item from list1 AND has 1+ item from list2 AND has 1+ item from list3, THEN keep the post
下面是我作爲一個開始,但它的方式是目前我需要大量的IF
語句賬戶,如果有人沒有按」 t從列表中選擇任何內容。我知道使用開關可能會更容易,但我不完全確定我的邏輯是否正確。
$(".post-link").each(function(index){
//Get all the post's terms from its hidden tag div and store in an array
var terms = $(this).find(".tags").attr('class').split(" ");
//Cut off the first two items ('hidden' and 'tags')
terms.splice(0,2);
//If interests is set
if(typeof interests[0] != 'undefined'){
var found = 0;
var keep = false;
//For each of the selected interests...
$.each(interests, function(index, value){
//For each of the posts terms
$.each(terms, function(index2, value2){
//If the posts has a selected interest, keep it
if(value == value2){ keep=true;}
});
});
//After all that, if we couldn't find anything...
if(keep!=true){
//Hide the post (.post-link)
$(this).hide();
}
}
//THE ABOVE ONLY ACCOUNTS FOR IF SOMETHING IS SELECTED FOR THE FIRST LIST
//I'M NOT SURE HOW I WOULD IMPLEMENT THIS ACROSS TWO OTHER LISTS
});
如果您需要更多信息,請讓我知道。
謝謝!