使用 jQuery 筛选具有 on data 属性的表 [英] Filter table with on data attribute with jQuery

查看:44
本文介绍了使用 jQuery 筛选具有 on data 属性的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据数据属性过滤表,而不是根据 td 标签内的值.

I am trying to filter a table based on data attribute, instead on value inside the td tag.

问题是,我无法让它工作,因为我总是收到这个错误:

The problem is, that I can't get it to work, because I always get this error:

未捕获的类型错误:无法调用未定义的方法匹配"

$(document).ready(function(){
    var elemens = $("td")
    searchInput = $("#search")
    searchInput.on('keyup',function(){

        elemens.each(function(){

            var re = new RegExp(searchInput.val(), 'gi');
            if( $(this).data('gui').match(re) === null )
            {
                $(this).parent('tr').hide();
            }else{
                $(this).parent('tr').show();
            }

        });                
    });
});​

我的小提琴:http://jsfiddle.net/T57ba/3/

推荐答案

数据属性在 tr 上而不是 td,而且 .data() 将转换适用的类型,在这种情况下是数字.而是使用 .attr()

The data attributes are on the tr not the td, also .data() will convert the applicable types, in this case numbers. Instead use .attr()

$(document).ready(function(){
    var elemens = $("tr")
    searchInput = $("#search")
    searchInput.on('keyup',function(){            
        elemens.each(function(){                
            var re = new RegExp(searchInput.val(), 'gi');
            if( $(this).attr('data-gui').match(re) === null ){
                $(this).hide();
            }
            else{
                $(this).show();
            }

        });                
    });
});​

演示

这篇关于使用 jQuery 筛选具有 on data 属性的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆