Filter table with on data attribute with jQuery

醉酒当歌 提交于 2021-01-28 10:48:51

问题


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:

Uncaught TypeError: Cannot call method 'match' of undefined

$(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();
            }

        });                
    });
});​

My Fiddle: http://jsfiddle.net/T57ba/3/


回答1:


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();
            }

        });                
    });
});​

DEMO



来源:https://stackoverflow.com/questions/14048718/filter-table-with-on-data-attribute-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!