0

jqueryを使用して検索してテーブルデータをフィルタリングするためのコードを作成しました。ただし、検索では大文字と小文字が区別されます。大文字と小文字を区別しないようにする必要があります。検索すると、文字列と文字列aの両方が表示されます。テーブルコードは -aA

<input id="searchInput" value="Type To Filter">
<br/>
<table>
    <thead>
        <tr>
            <th>Column1</th>
            <th>Column2</th>
        </tr>
    </thead>
    <tbody id="fbody">
        <tr>
            <td>Cat</td>
            <td>one</td>
        </tr>
        <tr>
            <td>dog</td>
            <td>two</td>
        </tr>
        <tr>
            <td>cat</td>
            <td>Three</td>
        </tr>
        <tr>
            <td>moose</td>
            <td>four</td>
        </tr>
        <tr>
            <td>mouse</td>
            <td>five</td>
        </tr>
        <tr>
            <td>dog</td>
            <td>six</td>
        </tr>
    </tbody>
</table> 

そしてJSコードは -

$("#searchInput").keyup(function () {
    //split the current value of searchInput
    var data = this.value.split(" ");
    //create a jquery object of the rows
    var jo = $("#fbody").find("tr");
    if (this.value == "") {
        jo.show();
        return;
    }
    //hide all the rows
    jo.hide();

    //Recusively filter the jquery object to get results.
    jo.filter(function (i, v) {
        var $t = $(this);
        for (var d = 0; d < data.length; ++d) {
            if ($t.is(":contains('" + data[d] + "')")) {
                return true;
            }
        }
        return false;
    })
    //show the rows that match.
    .show();
}).focus(function () {
    this.value = "";
    $(this).css({
        "color": "black"
    });
    $(this).unbind('focus');
}).css({
    "color": "#C0C0C0"
});
4

2 に答える 2

4

この条件を置き換えます。

if ($t.is(":contains('" + data[d] + "')")) {
    return true;
}

この条件で:

if ($t.text().toLowerCase().indexOf(data[d].toLowerCase()) > -1) {
    return true;
}

jsFiddle の例

于 2013-11-06T16:33:33.263 に答える