【问题标题】:How can i search inside the box without clicking enter如何在不单击输入的情况下在框内搜索
【发布时间】:2021-12-05 13:09:23
【问题描述】:

我从网站获取 .json 数据并将该数据打印为列表。例如,当我在搜索框中输入“a”时,我希望它过滤列表并在表格中显示结果。

到目前为止,我已经编写了从 w3 学校平台获取过滤器的代码,但由于某种原因我无法过滤列表。

   <!DOCTYPE html>

< html >

< head >

< meta name="viewport" content="width=device-width, initial-scale=1" >

< style >

 *{
  box-sizing: border-box;
}

#myInput 

{

  background-image: url('/css/searchicon.png');

  background-position: 10px 10px;

  background-repeat: no-repeat;

  width: 100%;

  font-size: 16px;

  padding: 12px 20px 12px 40px;

  border: 1px solid #ddd;

  margin-bottom: 12px;

}

#myTable {
  border-collapse: collapse;
  width: 100%;
  border: 1px solid #ddd;
  font-size: 18px;
}

#myTable th, #myTable td {
  text-align: left;
  padding: 12px;
}

#myTable tr {
  border-bottom: 1px solid #ddd;
}

#myTable tr.header, #myTable tr:hover {
  background-color: #f1f1f1;
}
< /style >

</ head >
< body >



< input type="text" id="myInput" onkeyup="myFunction()" placeholder="Ara.." title="arama yapın" >

< ?php


echo " < table id=MyTable > ";

echo " < tr class=header > ";

echo " < th>Id< /th >";
  
echo  "< th> < pre > < /pre > < /th >";

echo "< th> Bin</ th>";

echo  "< th> < pre > < /pre > < /th >";

echo "< th>Tür< /th >";

echo  "< th > < pre > < /pre>< /th>";

echo "< th>Banka Adı< /th>" ;

echo  "< th> < pre> < /pre> < /th>";

echo "< th> Type < /th>";

echo  "< th> < pre> < /pre> < /th>";

echo "< th> Name < /th>";

echo  "< th>  < pre> < /pre> < /th>";

echo "< th> Oluşturma Tarihi < /th>";

echo  "< th> < pre>  < /pre> < /th>";

echo "< th> Güncelleme Tarihi < /th>";

echo "< /tr >";



$json_url = "websitename";

$json = file_get_contents($json_url);

$data = json_decode($json, TRUE);



foreach($data as $record) 

{

 echo " < tr> ";

 echo "< td>" .$record["id"]. "< td>";

 echo "< td>" .$record["bin"]. "< td>";

 echo "< td>" .$record["tur"]. "< td>";

 echo "< td>" .$record["banka_adi"]. "< td>";

 echo "< td>" .$record["type"]. "< td>";

 echo "< td>" .$record["name"]. "< td>";

 echo "< td>" .$record["created_at"]. "< td>";

 echo "< td>" .$record["updated_at"]. "< td>";

 echo "< /tr>";


}
echo "< /table>";


?>

< script>
function myFunction() 

{

  var input, filter, table, tr, td, i, txtValue;


  input = document.getElementById("myInput");

  filter = input.value.toUpperCase();

  table = document.getElementById("myTable");

  tr = table.getElementsByTagName("tr");

  for (i = 0; i < tr.length; i++) 
  {
td = tr[i].getElementsByTagName("td")[5];
    if (td) 
    {
 txtValue = td.textContent || td.innerText;

 if (txtValue.toUpperCase().indexOf(filter) > -1) 
      {
        tr[i].style.display = "";
      } 

 ​else


{
 ​tr[i].style.display = "none";
​}

   ​}       
 ​} 

 }

    
< /script>

【问题讨论】:

  • 请点击edit,然后点击[&lt;&gt;],并在minimal reproducible example 上发布您的努力。你的代码搞砸了。如果这不是 PHP 问题,请仅发布 HTML、JS 和 CSS
  • 非常感谢您的帮助,我仍在努力尽快学习规则。
  • 为什么你有一个带有
     的标题?如果您想要更多间距,请使用边距
  • 我更新了我的答案。您的 ID 中有错字,javascript 中有一些十六进制字符

标签: javascript php html


【解决方案1】:
  1. 我的表,不是我的表
  2. 在您的 JavaScript 中使用松散的 Hex 0A

代码可以简化

window.addEventListener("load", () => {
  document.getElementById("myInput").addEventListener("input", e => {
    const filter = e.target.value.toUpperCase();
    document.querySelectorAll("#MyTable tbody tr")
      .forEach(row => {
        row.hidden = filter && !row.cells[5].textContent.toUpperCase().includes(filter)
      })
  })
})

window.addEventListener("load", () => {
  document.getElementById("myInput").addEventListener("input", e => {
    const filter = e.target.value.toUpperCase();
    document.querySelectorAll("#MyTable tbody tr")
      .forEach(row => {
        row.hidden = filter && !row.cells[5].textContent.toUpperCase().includes(filter)
      })
  })
})
* {
  box-sizing: border-box;
}

#myInput {
  background-image: url('/css/searchicon.png');
  background-position: 10px 10px;
  background-repeat: no-repeat;
  width: 100%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 1px solid #ddd;
  margin-bottom: 12px;
}

#myTable {
  border-collapse: collapse;
  width: 100%;
  border: 1px solid #ddd;
  font-size: 18px;
}

#myTable th,
#myTable td {
  text-align: left;
  padding: 12px;
}

#myTable tr {
  border-bottom: 1px solid #ddd;
}

#myTable tr.header,
#myTable tr:hover {
  background-color: #f1f1f1;
}
<input type="text" id="myInput" placeholder="Ara.." title="arama yapın" autocomplete="off" />
<table id="MyTable">
  <thead>
    <tr class="header">
      <th>Id</th>
      <th>Bin</th>
      <th>Tür</th>
      <th>Banka Adı</th>
      <th>Type</th>
      <th>Name</th>
      <th>Oluşturma Tarihi</th>
      <th>Güncelleme Tarihi</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Id</td>
      <td>Bin</td>
      <td>Tür</td>
      <td>Banka Adı</td>
      <td>Type</td>
      <td>Name AAAA</td>
      <td>Oluşturma Tarihi</td>
      <td>Güncelleme Tarihi</td>
    </tr>
    <tr>
      <td>Id</td>
      <td>Bin</td>
      <td>Tür</td>
      <td>Banka Adı</td>
      <td>Type</td>
      <td>Name BBBB/td>
      <td>Oluşturma Tarihi</td>
      <td>Güncelleme Tarihi</td>
    </tr>
    <tr>
      <td>Id</td>
      <td>Bin</td>
      <td>Tür</td>
      <td>Banka Adı</td>
      <td>Type</td>
      <td>Name CCCC</td>
      <td>Oluşturma Tarihi</td>
      <td>Güncelleme Tarihi</td>
    </tr>
  </tbody>
</table>

这是你的固定版本

function myFunction() {
  var input, filter, table, tr, td, i, txtValue;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("myTable");
  tr = table.getElementsByTagName("tr");
  console.log(filter)
  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[5];
    if (td) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }
  }
}
* {
  box-sizing: border-box;
}

#myInput {
  background-image: url('/css/searchicon.png');
  background-position: 10px 10px;
  background-repeat: no-repeat;
  width: 100%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 1px solid #ddd;
  margin-bottom: 12px;
}

#myTable {
  border-collapse: collapse;
  width: 100%;
  border: 1px solid #ddd;
  font-size: 18px;
}

#myTable th,
#myTable td {
  text-align: left;
  padding: 12px;
}

#myTable tr {
  border-bottom: 1px solid #ddd;
}

#myTable tr.header,
#myTable tr:hover {
  background-color: #f1f1f1;
}
<input type="text" id="myInput" placeholder="Ara.." onkeyup="myFunction()" title="arama yapın" autocomplete="off" />
<table id="myTable">
  <thead>
    <tr class="header">
      <th>Id</th>
      <th>Bin</th>
      <th>Tür</th>
      <th>Banka Adı</th>
      <th>Type</th>
      <th>Name</th>
      <th>Oluşturma Tarihi</th>
      <th>Güncelleme Tarihi</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Id</td>
      <td>Bin</td>
      <td>Tür</td>
      <td>Banka Adı</td>
      <td>Type</td>
      <td>Name AAAA</td>
      <td>Oluşturma Tarihi</td>
      <td>Güncelleme Tarihi</td>
    </tr>
    <tr>
      <td>Id</td>
      <td>Bin</td>
      <td>Tür</td>
      <td>Banka Adı</td>
      <td>Type</td>
      <td>Name BBBB</td>
      <td>Oluşturma Tarihi</td>
      <td>Güncelleme Tarihi</td>
    </tr>
    <tr>
      <td>Id</td>
      <td>Bin</td>
      <td>Tür</td>
      <td>Banka Adı</td>
      <td>Type</td>
      <td>Name CCCC</td>
      <td>Oluşturma Tarihi</td>
      <td>Güncelleme Tarihi</td>
    </tr>
  </tbody>
</table>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    • 2014-11-20
    相关资源
    最近更新 更多