Search HTML table with JS and jQuery

前端 未结 5 1479
谎友^
谎友^ 2020-12-13 11:28


I made a table and wanted to make it searchable, so I googled and looked here at starckoverflow.
But somehow, the things I\'ve found, that should work, dont wo

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-13 12:09

    The ways posted were a little slow for my table. I came up with a different solution that seems to be much faster.

    If you want to search through every cell you can add an attribute to the cell (I used data-name), ex:John Smith. Then you can use this javascript code:

    $("#search").keyup(function() {
      var val = this.value.trim().toLowerCase();
      if ('' != val) {
        var split = val.split(/\s+/);
        var selector = 'td';
        for(var i=0;i

    If you just want to search a rows for a single attribute you can just add the attribute to the row like John Smith... and use the following:

    $("#search").keyup(function() {
      var val = this.value.trim().toLowerCase();
      if ('' != val) {
        var split = val.split(/\s+/);
        var selector = 'tr';
        for(var i=0;i

    Hope that helps!

提交回复
热议问题