jQuery table 行过滤器 - 同时隐藏和显示

jQuery table row filter - hide and show at the same time

我有一个 table,其中包含一个包含关键字的列和一个 select 字段(css class .filter),其中包含与这些关键字相对应的选项和值.

它工作正常,直到我将 select 字段更改为另一个选项。使用以下代码,一切都显然被隐藏了。但是在隐藏不起作用之前为每一行添加一个 .show() (不再隐藏任何内容)。隐藏后显示 selected 行也不起作用。

$(".filter").change(function () {
        if ($(this).val() != "all") {
            $("tbody").find("tr:not(:contains('"+$(this).val()+"'))").hide(); //hide everything except what contains the selected option

        } else {
            $("tbody").find("tr:hidden").show(); // reset the filter and show everything
        }
    });

您可以隐藏所有行,然后仅显示过滤后的项目

$("tbody").find("tr").hide().filter(":contains('" + $(this).val() + "')").show();