【问题标题】:Filter based on combination of multiple select and single select elements基于多选和单选元素组合的过滤器
【发布时间】:2013-09-01 00:31:58
【问题描述】:

我想按类过滤(显示/隐藏)元素。我想让 5 个过滤器一起工作。

<select id='filter4'>...</select>
<select id='filter5'>...</select>
<select id='filter6'>...</select>
<select id='filter7'>...</select>

<select id='filter6' multiple>...</select>

jquery:

jQuery(document).ready(function ($) {
    $("select").on("change", function () {
        var filterVal = $("select#filter4").val();
        var filterVal2 = $("select#filter5").val();
        var filterVal3 = $("select#filter6").val();
        var filterVal4 = $("select#filter7").val();
        var filterVal5 = $("select#filter8").val();
        var filterSelector = "";
        var filter2Selector = "";
        var filter3Selector = "";
        var filter4Selector = "";
        var filter5Selector = "";

        if (filterVal == "all" && filterVal2 == "all" && filterVal3 == "all" && filterVal4 == "all" && filterVal5 === null) {
            //show all items 
            $(".item").fadeIn("fast");
        } else {
            if (filterVal != "all") {
                filterSelector = "." + filterVal;
            }

            if (filterVal2 != "all") {
                filter2Selector = "." + filterVal2;
            }

            if (filterVal3 != "all") {
                filter3Selector = "." + filterVal3;
            }

            if (filterVal4 != "all") {
                filter4Selector = "." + filterVal4;
            }

            if (filterVal5 !== null) {
                filter5Selector = "." + filterVal5;
            }

            $(".item").hide();
            $(filterSelector + filter2Selector + filter3Selector + filter4Selector + filter5Selector).fadeIn("fast");
        }

    });
});

everything in this jsfiddle

前四个单一过滤器可以解决任何问题。选择多个选项时,第五个多选择元素无法正常工作(过滤)。

编辑:需要借助全局匹配的 g 修饰符 (/ /g) 将逗号替换为点(多个 css 选择器):

$(filter4Selector + filter5Selector.replace(/,/g, ".")).fadeIn("fast");

http://jsfiddle.net/mahish/dum7n/

下面的答案提供了同样有效的不同代码!

【问题讨论】:

    标签: jquery filtering selection


    【解决方案1】:

    清理了您的代码并修复了多个查询。请注意:

    .class1.class2.class3 表示class1 &amp;&amp; class2 &amp;&amp; class 3

    .class1, .class2, .class3 表示class1 || class2 || class 3

    这是之前在 cmets 中混乱的原因。

    jQuery(document).ready(function ($) {
    
        var values = [7, 8];
    
        $("select").on("change", function () {
    
            var showAll = true,
                show = [],
                joined;
    
            $.each(values, function (index, id) {
                var $el = $('#filter' + id),
                    multi = $el.attr('multiple'),
                    val = $el.val();
    
                if (multi) {
                    if (val !== null) {
                        showAll = false;
                        $.each(val, function (i, v) {
                            show.push( v );
                        });
                    }
                } else {
                    if (val != 'all') {
                        showAll = false;
                        show.push( val );
                    }
                }
    
    
            });
    
            console.log(showAll);
            console.log(show);
    
            if (showAll) {
                //show all items 
                $(".item").fadeIn("fast");
            } else {
    
                joined = '.' + show.join('.');
    
                console.log( joined );
    
                $(".item").hide();
                $( joined ).fadeIn("fast");
            }
    
        });
    
        $.each(values, function (index, id) {
            $('#filter' + id).chosen();
        });
    
    
    });
    

    http://jsfiddle.net/9SZkr/1/

    【讨论】:

    • 哇,这很整洁。对我来说很好的学习曲线。虽然,多种选择仍然不起作用。我应该更改语言。
    • 我没有正确添加多选值
    • 实际上,整个过滤器现在根本不起作用:( 将 html 从这个小提琴复制到有字母而不是捷克语单词以清楚地看到结果。jsfiddle.net/mahish/Bd8S9
    • 保持控制台打开,您将看到它应该显示的所有类。你的标记有很多 em 所以对我来说是另一个挑战:)
    • 你只需要多值获取中的$.each.. 14 有它,其他也有
    【解决方案2】:

    用逗号分隔选择器,这样应该可以工作(谷歌 jQuery 多个选择器获取更多信息)

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 2017-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多