提问者:小点点

JavaScript/jQuery基于多个选择筛选结果


我试图为一个问题找到一个解决方案,在这个问题上,我需要根据多个标准来细化结果。我有多个选择标准:

  1. 水果-苹果橙子葡萄
  2. 蔬菜-番茄土豆西兰花
  3. 坚果-核桃花生杏仁
  4. 谷物-玉米小麦大米
  5. 香料-肉桂姜椒

用户只能从每个组中选择一个标准,例如,您可以选择苹果,但不能选择橘子或葡萄。参见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。目前只根据一个条件(关键字)过滤结果,但我需要包含其他条件。


共1个答案

匿名用户

用下面提到的脚本替换整个JavaScript/jQuery脚本,并根据ur需求检查更改

$(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://jsfidle.net/lsmxc/32/”。请在你那边查一下。