0

テーブルを 3 つのフィールドでフィルター処理したいのですが、3 つすべてがオプションで、1 つまたは 2 つまたは 3 つのフィールドで検索できます。少なくとも 1 つのフィールドを設定する必要があります。

これは私が到達したものです。このコードは1つのフィールドに対して完全に正常に機能し、他のフィールドをこの方法で追加すると、設定する必要があります。任意性をどのように導入できますか?

    $('#submitButton').click(function() {
    var quantity = $("[name='quantity']").val();
    var category = $('select.categorylist option:selected').val();
    var brand = $("[name='brand']").val();

     $("table tbody tr").hide();

    $("table tbody tr").each(function()
        {
            if ($(this).find("td:eq(3):contains('" + quantity + "')").html() != null)
                {
                    $(this).show();
                }
         });
    return false ; 

});
4

1 に答える 1

1

filter次のような関数を使用できます。

$('#submitButton').click(function() {

    var quantity = $("[name='quantity']").val(),
        category = $('select.categorylist option:selected').val(),
        brand = $("[name='brand']").val();

    $("table tbody tr:not(.header, .filters)").hide().filter(function() {
        var q = quantity ? this.cells[0].innerHTML === quantity : true,
            c = category ? this.cells[1].innerHTML === category : true,
            b = brand ? this.cells[2].innerHTML === brand : true;
        return q && c && b;
    }).show();

    return false;
});

http://jsfiddle.net/v4HYP/

したがって、各列の値が対応するフィルターに一致する場合にのみ、行が表示されます。

于 2013-04-08T12:53:03.747 に答える