【问题标题】:Table search Javascript and jQuery CSS表格搜索 Javascript 和 jQuery CSS
【发布时间】:2014-01-07 12:45:07
【问题描述】:

由于玩家名单很长,我想按玩家姓名、国籍等进行搜索。要使用什么 Javascript 和 jQuery,是否需要输入任何 CSS?我尝试了各种文章,但都行不通。

<input type="text" id="search" placeholder="Search Player"></input>

<table class="playersindex">
    <thead>
        <tr>
            <th class="wrapper-top" colspan="7">PLAYERS INDEX / 2013-14 / <span style="color: #ff3333;">SSSC</span></th>
        </tr>
        <tr>
            <th width="23">NO.</th>
            <th class="player">PLAYER</th>
            <th></th>
            <th width="105">POSITION</th>
            <th width="30">AGE</th>
            <th width="105">NATIONALITY</th>
            <th width="105">FIRST SEASON</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class="number">1</td>
            <td class="player"><a href="http://www.premierleague.com/en-gb/players/profile.html/chuba-akpom">Kevin Siew</a></td>
            <td class="playerphoto"></td>
            <td>Goalkeeper</td>
            <td>23</td>
            <td>Singaporean</td>
            <td>2010/11</td>
        </tr>
        <tr>
            <td class="number">2</td>
            <td class="player"><a href="http://www.premierleague.com/en-gb/players/profile.html/chuba-akpom">Mohamad Hairul</a> (C)</td>
            <td class="playerphoto"></td>
            <td>Goalkeeper</td>
            <td>23</td>
            <td>Singaporean</td>
            <td>2010/11</td>
        </tr>
        <tr>
            <td class="number">3</td>
            <td class="player"><a href="http://www.premierleague.com/en-gb/players/profile.html/chuba-akpom">Nazir Samaluddin</a></td>
            <td class="playerphoto"></td>
            <td>Goalkeeper</td>
            <td>23</td>
            <td>Singaporean</td>
            <td>2010/11</td>
            ....
        <tr>
            <th width="23">NO.</th>
            <th class="player">PLAYER</th>
            <th></th>
            <th width="105">POSITION</th>
            <th width="30">AGE</th>
            <th width="105">NATIONALITY</th>
            <th width="105">FIRST SEASON</th>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <th class="wrapper-bottom" colspan="7">LAST UPDATED:  10 December 2013, 1720</th>
        </tr>
    </tfoot>
</table>

<script type="text/Javascript">
    $("#search").keyup(function() {
        var value = this.value;

        $("table.playersindex").find("tr").each(function(index) {
            if (!index) return;
            var id = $(this).find("td").first().text();
            $(this).toggle(id.indexOf(value) !== -1);
        });
    });
</script>

【问题讨论】:

    标签: javascript jquery search html-table


    【解决方案1】:

    试试这个

    $("#search").keyup(function () {
        var value = this.value.toLowerCase().trim();
    
        $("table tr").each(function (index) {
            if (!index) return;
            $(this).find("td").each(function () {
                var id = $(this).text().toLowerCase().trim();
                var not_found = (id.indexOf(value) == -1);
                $(this).closest('tr').toggle(!not_found);
                return not_found;
            });
        });
    });
    

    DEMO

    【讨论】:

    • 已更新但它似乎不起作用(尽管它在您的演示中起作用)。 sefcl.sg/players/singapore-spurs-sc
    • 我会检查并评论你
    • 你把这个脚本放在了哪个文件?
    • 我不能只包含在 HTML 正文中吗?有一件事我显然不明白我应该在哪里包含 js 文件。
    • 哦.. 请在上面包含此代码 &lt;script type="text/javascript"&gt; $(window).load(function(){ $("#search").keyup(function () { var value = this.value.toLowerCase().trim(); $("table tr").each(function (index) { if (!index) return; $(this).find("td").each(function () { var id = $(this).text().toLowerCase().trim(); var not_found = (id.indexOf(value) == -1); $(this).closest('tr').toggle(!not_found); return not_found; }); }); }); }); &lt;/script&gt;
    猜你喜欢
    • 1970-01-01
    • 2011-03-03
    • 2016-05-01
    • 2015-08-18
    • 2010-10-24
    • 2015-12-19
    • 2015-11-07
    • 1970-01-01
    • 2021-03-09
    相关资源
    最近更新 更多