我正在尝试找到一个问题的解决方案,我需要根据多个条件优化结果。 我有多个选择标准:
用户只能从每个组中选择一个条件,例如,您可以选择苹果,但不能选择橙子或葡萄。请参阅jsfiddle。
然后我有多个包含不同项目的篮子。
<div class="results">
<div class="basket" data-keywords="apple broccoli peanut rice cinnamon">Result1</div>
<div class="basket" data-keywords="apple tomato almond wheat pepper">Result2</div>
<div class="basket" data-keywords="orange potato peanut rice ginger">Result3</div>
<div class="basket" data-keywords="apple potato walnut corn ginger">Result4</div>
<div class="basket" data-keywords="grape broccoli peanut corn ginger">Result5</div>
</div>
但问题是必须根据多个标准改进这些结果。例如,我选择了苹果,然后是番茄,所以我需要优化结果,用户只能看到苹果和番茄的结果。
这是我的jsfiddle。目前,结果仅根据一个标准(关键字)进行过滤,但我需要以某种方式包含其他标准。
答案 0 :(得分:0)
使用下面提到的脚本替换整个javascript / Jquery脚本,并使用您的需求检查更改
$(document).ready(function(){
$('a.sortLink1').on('click',function(event){
event.preventDefault(event);
if ($(this).hasClass('selected')) {
$(this).removeClass('selected');
var SelectedAry = $(".selected");
var flag = false;
if(SelectedAry != undefined && SelectedAry.length == 0)
flag = true
sortResult(flag);
}
else{
$('a.sortLink1').removeClass('selected');
$(this).addClass('selected');
var SelectedAry = $(".selected");
var flag = false;
if(SelectedAry != undefined && SelectedAry.length == 0)
flag = true
sortResult(flag);
}}
);
$('a.sortLink2').on('click',function(event){
event.preventDefault(event);
if ($(this).hasClass('selected')) {
$(this).removeClass('selected');
var SelectedAry = $(".selected");
var flag = false;
if(SelectedAry != undefined && SelectedAry.length == 0)
flag = true
sortResult(flag);
}
else{
$('a.sortLink2').removeClass('selected');
$(this).addClass('selected');
var SelectedAry = $(".selected");
var flag = false;
if(SelectedAry != undefined && SelectedAry.length == 0)
flag = true
sortResult(flag);
}}
);
$('a.sortLink3').on('click',function(event){
event.preventDefault(event);
if ($(this).hasClass('selected')) {
$(this).removeClass('selected');
var SelectedAry = $(".selected");
var flag = false;
if(SelectedAry != undefined && SelectedAry.length == 0)
flag = true
sortResult(flag);
}
else{
$('a.sortLink3').removeClass('selected');
$(this).addClass('selected');
var flag = false;
if(SelectedAry != undefined && SelectedAry.length == 0)
flag = true
sortResult(flag);
}}
);
$('a.sortLink4').on('click',function(event){
event.preventDefault(event);
if ($(this).hasClass('selected')) {
$(this).removeClass('selected');
var SelectedAry = $(".selected");
var flag = false;
if(SelectedAry != undefined && SelectedAry.length == 0)
flag = true
sortResult(flag);
}
else{
$('a.sortLink4').removeClass('selected');
$(this).addClass('selected');
var SelectedAry = $(".selected");
var flag = false;
if(SelectedAry != undefined && SelectedAry.length == 0)
flag = true
sortResult(flag);
}}
);
$('a.sortLink5').on('click',function(event){
event.preventDefault(event);
if ($(this).hasClass('selected')) {
$(this).removeClass('selected');
var SelectedAry = $(".selected");
var flag = false;
if(SelectedAry != undefined && SelectedAry.length == 0)
flag = true
sortResult(flag);
}
else{
$('a.sortLink5').removeClass('selected');
$(this).addClass('selected');
var SelectedAry = $(".selected");
var flag = false;
if(SelectedAry != undefined && SelectedAry.length == 0)
flag = true
sortResult(flag);
}}
);
$('.basket').addClass('showMe');
positionThumbnails();
});
function sortResult (flag){
$('.results .basket').each(function(){
var thumbnailKeywords = $(this).attr('data-keywords');
if(flag){
$(this).addClass('showMe').removeClass('hideMe');
}else{
var mymatch = false;
var SelectedAry = $(".selected");
SelectedAry.each(function(){
var newKeyword = $(this).attr("data-keyword");
var test_regexp = new RegExp("\\b"+newKeyword+"\\b");
mymatch = thumbnailKeywords.match(test_regexp);
if(!mymatch)
return false;
});
if(mymatch){
$(this).addClass('showMe').removeClass('hideMe');
}else{
$(this).addClass('hideMe').removeClass('showMe');
}
}
});
positionThumbnails();
}
function positionThumbnails(){
$('.basket.hideMe').animate({opacity:0},500,function(){
$(this).css({'display':'none'});
});
$('.basket.showMe').each(function(index){
$(this).css('display','block').animate({opacity:1},500)
});
}
请检查您的要求并比较您的脚本。为了知识 jsfiddle请访问“http://jsfiddle.net/LSMxC/32/”。请在最后检查。
答案 1 :(得分:0)
这是一个相对较短的js脚本,用于解析匹配的html:
$(document).ready(function(){
$(".sorting > div a").click(function(){
$(this).siblings().removeClass('selected');
if ($(this).hasClass('selected')) {
$(this).removeClass('selected');
} else {
$(this).addClass('selected');
}
$('.results .basket').each(function(){
var basketKeywords = $(this).attr('data-keywords');
var match = true;
$(".sorting > div a.selected").each(function(){
if (!basketKeywords.match($(this).attr('data-keyword'))){
match = false;
}
});
if (match){
$(this).addClass('showMe').removeClass('hideMe');
} else {
$(this).addClass('hideMe').removeClass('showMe');
}
});
positionThumbnails();
});
});
function positionThumbnails(){
$('.basket.hideMe').animate({opacity:0},500,function(){
$(this).css({'display':'none'});
});
$('.basket.showMe').each(function(index){
$(this).css('display','block').animate({opacity:1},500)
});
}