【问题标题】:jQuery, check classes against arrayjQuery,根据数组检查类
【发布时间】:2015-09-05 18:03:53
【问题描述】:

这个问题的标题可能会产生误导,但我想不出更好的措辞。

让我进入正题。我正在使用按钮来过滤状态表。如果我有按钮 A 和按钮 B 处于活动状态,我只想显示 A 和 B,其余的都是隐藏的。目前我只能让它显示 A 或 B,不能同时显示。

var aSelected = [];
$(".admin_view #filterTable tr td button").on("click", function() {
    $(this).toggleClass("selected"); //class to show user button is active
    //Add to array if not in already
    if ($.inArray(this.id,aSelected) == -1) {
        aSelected.push(this.id);
    //Remove from array if button is not active
    } else if ($.inArray(this.id,aSelected) >= 0) {
        aSelected.splice(aSelected.indexOf(this.id), 1);
    }
    //Show all if array is empty
    if (!aSelected.length) {
        $(".admin_view #applicantTable tbody tr").each(function() {
            $(".admin_view #applicantTable tbody tr").removeClass("hidden");
        });
    }
    //This is the section I need help with
    $.map(aSelected, function(a) {
        $(".admin_view #applicantTable tbody tr").not("."+a).addClass("hidden");
        $(".admin_view #applicantTable tbody tr."+a).removeClass("hidden");
    });
});

我需要找到一种方法来与整个数组进行比较,而不是与一个部分进行比较。

【问题讨论】:

    标签: javascript jquery arrays class


    【解决方案1】:

    目前,您在 aSelected 中隐藏 每个项目 的所有其他元素,而不是仅在开头。如果您首先隐藏所有内容,然后显示您想要的元素,它应该可以工作:

    //This is the section I need help with
    $(".admin_view #applicantTable tbody tr:not(.hidden)").addClass("hidden");
    $.map(aSelected, function(a) {
        $(".admin_view #applicantTable tbody tr.hidden."+a).removeClass("hidden");
    });
    

    【讨论】:

    • 我感觉这只是我的逻辑问题。这个答案很好用,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多