问题
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