donglei_0507 2013-09-12 06:08
浏览 70
已采纳

实时搜索过滤标签td

i have problem with my live search the filtering is working but i want is only the <td> tags can filter and the <th> is not belong.

this is my html

<input />
 <table class="AvailableGroupLab availGrpLabs avalLabs">
    <tr>
        <th>Name</th>
        <th>Age</th>
    </tr>

    <tr>
        <td><span>wewe</span>

        </td>
        <td>16</td>
    </tr>
    <tr>
        <td><span>Melvin</span>

        </td>
        <td>18</td>
    </tr>
    <tr>
        <td><span>Marvin</span>

        </td>
        <td>20</td>
    </tr>
</table>

and the javascript

function filter(element) {
    var $trs = $('.AvailableGroupLab tr').hide();
    var regexp = new RegExp($(element).val(), 'i');

    var $valid = $trs.filter(function () {
        return regexp.test($(this).find('td:first-child').text())
    }).show();

    $trs.not($valid).hide()
}

$('input').on('keyup change', function () {
    filter(this);
})
  • 写回答

6条回答 默认 最新

  • dongtui2029 2013-09-12 06:16
    关注

    Try this:

    function filter(element) {
    var $trs = $('.AvailableGroupLab tr').not(":first").hide(); //don't hide first tr 
    var regexp = new RegExp($(element).val(), 'i');
    
    var $valid = $trs.filter(function () {
        return regexp.test($(this).find('td:first-child').text())
    }).show();
    
    $trs.not($valid).hide()
    }
    
    $('input').on('keyup change', function () {
        filter(this);
    })
    

    Fiddle Here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度