Javascript jQuery逻辑过滤
我有一个贴子链接列表,在一个内部分区中有标签。用户从三个不同的列表中选择来过滤贴子 基本上,我想要实现的是基于用户选择的三个列表内容的前端过滤 我希望逻辑基本上是这样的:如果post tags list从list1中有1+项,从list2中有1+项,从list3中有1+项,那么保留post 下面是我的开始,但按照目前的方式,如果有人没有从列表中选择任何内容,我需要大量的IF语句来解释。我知道使用开关可能会更容易,但我不完全确定我的逻辑是否正确Javascript jQuery逻辑过滤,javascript,jquery,filter,logic,Javascript,Jquery,Filter,Logic,我有一个贴子链接列表,在一个内部分区中有标签。用户从三个不同的列表中选择来过滤贴子 基本上,我想要实现的是基于用户选择的三个列表内容的前端过滤 我希望逻辑基本上是这样的:如果post tags list从list1中有1+项,从list2中有1+项,从list3中有1+项,那么保留post 下面是我的开始,但按照目前的方式,如果有人没有从列表中选择任何内容,我需要大量的IF语句来解释。我知道使用开关可能会更容易,但我不完全确定我的逻辑是否正确 $(".post-link").each(funct
$(".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
});
如果你需要更多信息,请告诉我
谢谢 好吧,我找到了解决问题的办法。基本上,对于每一篇文章,我设置了一个show变量,并将其设置为true。除非另有证明,否则我们希望显示文章。因此,我检查我的三个列表中是否每个列表都有内容,如果有,然后在foreach中执行foreach,以检查我的一个post术语是否与我选择的项目匹配。如果有,则保持show=true,否则为false。对其他两个列表执行此操作。在这三个检查之后,如果show仍然为true,那么它必须在每三个字段中至少有一个项目(如果三个字段适用),如果为false,则隐藏帖子 代码如下:
$(".post-link").each(function(index){
var show = true;
//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;
$.each(interests, function(index, value){
$.each(terms, function(index2, value2){
if(value == value2){ found++; }
});
});
if(found < 1){
show = false;
}
}
//If type is set
if(typeof type[0] != 'undefined'){
var found = 0;
$.each(type, function(index, value){
$.each(terms, function(index2, value2){
if(value == value2){ found++; }
});
});
if(found < 1){
show = false;
}
}
//If experience is set
if(typeof experience[0] != 'undefined'){
var found = 0;
$.each(experience, function(index, value){
$.each(terms, function(index2, value2){
if(value == value2){ found++; }
});
});
if(found < 1){
show = false;
}
}
if(!show){
$(this).hide();
}
});