如何将jQuery过滤器添加到html表?

问题描述 投票:2回答:2

我正在尝试使用JQuery将下拉过滤器放置到html表中。这是代码。

report.php

<table id ="myTable" class="table table-striped">

            <thead>
                <tr>
                    <th></th>
                    <th> First Name </th>
                    <th> Last Name </th>                  
                    <th> Phone </th>
                    <th> Email </th>              
                    <th> Gender</th>

                        <th>Term
                        <select id="filterText"  onchange='filterText()'>
                              <option disabled selected>Select</option>
                              <option value="all">All</option>
                              <option value="Fall2018">Fall2018</option> 
                              <option value="Srping2019">Spring2019</option>
                        </select></th>
                    <th> Action </th>

                </tr>
            </thead> 
            <?php
            if (count($report) === 0) {
                echo "<tr>No Reports</tr>";
            } else {

                for ($i = 0; $i < count($report); $i++) {

                    echo

                    "<tr class='row'>

                        <td>{$report[$i]['FName']}</td>
                        <td>{$report[$i]['LName']}</td>
                        <td>{$report[$i]['HPhone']}</td>
                        <td>{$report[$i]['PEmail']}</td>
                        <td>{$report[$i]['Gender']}</td>
                        <td>{$report[$i]['Term']}</td>

                        <td><a class=btn href='read.php?id=".$report[$i]['RegId']."'>Read</a></td>  

                    </tr>";

                }
            }



            ?>
        </table>

main.js

function filterText()
    {  
        var rex = new RegExp($('#filterText').val());
        if(rex ==="/all/"){clearFilter();}else{
            $('.row').show();
            $('.row').filter(function() {
            return rex.test($(this).text());
            }).hide();
    }
    }

   function clearFilter()
    {
        $('.filterText').val('');
        $('.row').show();
    }

我将下拉过滤器添加到term列。此代码给出相反的结果。就像我单击下拉菜单的“全部”选项时一样,它在结果中显示Spring2019。当我单击“ Spring2019”时,它将显示所有值。并且``2018年秋季''还显示了2019年春季的所有值。

您可以检查代码中的错误吗?

javascript jquery html-table dropdown
2个回答
1
投票
萨拉姆,我认为您可以按单元格文本而不是行文本进行过滤,只需将类添加到您的单元格中

0
投票
萨拉姆,在我这边没问题,尝试在此处运行此代码段,您会发现它工作正常,只是找到您生成的html中的区别是什么
© www.soinside.com 2019 - 2024. All rights reserved.