Live search through table rows

前端 未结 15 1860
失恋的感觉
失恋的感觉 2020-11-29 00:52

I want to do a live search through the table rows, using jQuery, the \"live\" word is the key, because I want to type the keywords in the text input, on the same site and I\

15条回答
  •  悲&欢浪女
    2020-11-29 01:30

    Hey for everyone still looking in 2020. I took some answers from here and made my own searchTable function.

    function searchTable() {
     var input, filter, table, tr, td, i, txtValue;
     input = document.getElementById("myInput");
     filter = input.value.toUpperCase();
     table = document.getElementById("showTable");
     tr = table.getElementsByTagName("tr");
     th = table.getElementsByTagName("th");
     var tdarray = [];
     var txtValue = [];
     for (i = 0; i < tr.length; i++) {
       for ( j = 0; j < th.length; j++) {
         tdarray[j] = tr[i].getElementsByTagName("td")[j];
       }
       if (tdarray) {
         for (var x = 0; x < tdarray.length; x++) {
           if (typeof tdarray[x] !== "undefined") {
              txtValue[x] = tdarray[x].textContent || tdarray[x].innerText;
              if (txtValue[x].toUpperCase().indexOf(filter) > -1) {
                tr[i].style.display = "";
              } else {
                tr[i].style.display = "none";
              }
           }
         }
       }
     }
    }
    
    
    
      
    
    
    

提交回复
热议问题