onkeyup函数删除了表头

时间:2017-09-28 05:32:39

标签: javascript php html

我有一个简单的搜索功能,可以过滤表格。它工作正常。 但是,它也隐藏了标题。如何从搜索中省略标题?任何帮助将不胜感激。

PHP / HTML:

    //Create a table to display the output 
    echo '<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">';
    echo '<div class="table-responsive">';
    echo '<table id="myTable"><tr bgcolor="#cccccc"><td>Name</td><td>E-Mail Address</td><td>Office Phone</td><td>Mobile</td></tr>';

    //Populate the table from LDAP
    echo "<tr><td>".$LDAP_FirstName." " .$LDAP_LastName."</td><td><a class='one' href='mailto:" .$LDAP_InternetAddress. "'>" .$LDAP_InternetAddress."</td><td>".$LDAP_OfficePhone."</td><td>".$LDAP_CellPhone."</td><tr>";
    echo("</table>");
    echo("</div>");

脚本:

function myFunction() {
 // Declare variables 
  var input, filter, table, tr, td, i;
   input = document.getElementById("myInput");
   filter = input.value.toUpperCase();
   table = document.getElementById("myTable");
   tr = table.getElementsByTagName("tr");

 // Loop through all table rows, and hide those who don't match the search query
  for (i = 0; i < tr.length; i++) {
   td = tr[i].getElementsByTagName("td")[0];
    if (td) {
      if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
    tr[i].style.display = "";
  } else {
    tr[i].style.display = "none";
      }
    } 
  }
}

1 个答案:

答案 0 :(得分:0)

您的标题将是第一个元素,因此请跳过for循环中的第一个元素

for (i = 1; i < tr.length; i++) //replace 0 with 1