【问题标题】:Filter table with on data attribute with jQuery使用 jQuery 过滤具有数据属性的表
【发布时间】:2012-12-12 11:47:50
【问题描述】:

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

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

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

$(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/

【问题讨论】:

    标签: jquery search filter attr


    【解决方案1】:

    数据属性在 tr 而不是 td 上,.data() 也将转换适用的类型,在这种情况下为数字。而是使用.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();
                }
    
            });                
        });
    });​
    

    DEMO

    【讨论】:

    • 谢谢,我看到了我用 td 和 tr 犯的错误,但是 .data() 会转换类型是什么意思?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    相关资源
    最近更新 更多