使用jquery函数和下拉选择过滤表

时间:2015-02-11 16:01:37

标签: jquery html

我正在尝试根据下拉列表中的选定值显示和隐藏行。用户选择user_name后,我可以使用按钮在表格中隐藏此用户行,但我想在选择后显示和隐藏行而不单击按钮,我无法使其工作。

这是我的工作功能,带有一个按钮:

$(document).ready(function() {
    $("button").click(function() {
        $("td").each(function(index, paragraph) {
            $td = $(paragraph);
            if ($td.html() === $('select[name=select1]').val()) {
                //hide the matched row rather than remove it
                $(this).parent("tr:first").hide();
            }
        });
        $('select[name="select1"]').on('change', function() {
            $("tr").show();
        });
    });
});

尝试避免使用按钮后,这是无法正常工作的功能:

$(document).ready(function() {
    $("selectedName").change(function() {
        $("td").each(function(index, paragraph) {
            $td = $(paragraph);
            if ($td.html() === $('select[name=select1]').val()) {
                //hide the matched row rather than remove it
                $(this).parent("tr:first").hide();
            }
        });
        $('select[name="select1"]').on('change', function() {
            $("tr").show();
        });
    });
});

在第二个问题上我做错了什么?

1 个答案:

答案 0 :(得分:0)

您的选择器在第二个代码块中无效:

$("selectedName")如果选择的名称是一个类,那么您需要$(".selectedName"),或者如果它的ID是$("#selectedName")