在 table jquery 中筛选日期/搜索日期

Filter Dates / Search dates in table jquery

我在一个名为 w3schools 的网站上找到了这段代码,它将过滤与输入字段的值匹配的任何文本值,下面是示例。

$(document).ready(function(){
  $("#myInput").on("keyup", function() {
    var value = $(this).val().toLowerCase();
    $("#myTable tr").filter(function() {
      $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
    });
  });
});
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>

<input id="myInput" type="text" placeholder="Search..">
<br><br>

<table>
  <thead>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Date</th>
  </tr>
  </thead>
  <tbody id="myTable">
  <tr>
    <td>Johnny</td>
    <td>Depp</td>
    <td>2022-06-03</td>
  </tr>
  <tr>
    <td>Angelina</td>
    <td>Jolie</td>
    <td>2022-06-04</td>
  </tr>
  <tr>
    <td>Peter</td>
    <td>Parker</td>
    <td>2022-06-05</td>
  </tr>
  <tr>
    <td>Lebron</td>
    <td>James</td>
    <td>2022-06-06</td>
  </tr>
  </tbody>
</table>
 

</body>
</html>

我的问题是我们可以在输入日期时这样做吗?我只是想搜索日期 我试图将类型更改为日期,但它不起作用,有什么方法可以做到这一点?

例如:

<input id="myInput" type="date" placeholder="Search..">

$(document).ready(function(){
  $("#myInput").on("keyup", function() {
    var value = $(this).val().toLowerCase();
    $("#myTable tr").filter(function() {
      $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
    });
  });
});
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>

<input id="myInput" type="date" placeholder="Search..">
<br><br>

<table>
  <thead>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Date</th>
  </tr>
  </thead>
  <tbody id="myTable">
  <tr>
    <td>Johnny</td>
    <td>Depp</td>
    <td>2022-06-03</td>
  </tr>
  <tr>
    <td>Angelina</td>
    <td>Jolie</td>
    <td>2022-06-04</td>
  </tr>
  <tr>
    <td>Peter</td>
    <td>Parker</td>
    <td>2022-06-05</td>
  </tr>
  <tr>
    <td>Lebron</td>
    <td>James</td>
    <td>2022-06-06</td>
  </tr>
  </tbody>
</table>
 

</body>
</html>

如果您使用键盘更改值,它可以正常工作

如果您希望它在选择日期时 运行 然后用 'change' 事件更改它:与

相同

$(document).ready(function(){
  $("#myInput").on("change", function() {
    let value = $(this).val().toLowerCase();
    console.log(value)
    $("#myTable tr").filter(function() {
      $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
    });
  });
});
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>

<input id="myInput" type="date" placeholder="Search..">
<br><br>

<table>
  <thead>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Date</th>
  </tr>
  </thead>
  <tbody id="myTable">
  <tr>
    <td>Johnny</td>
    <td>Depp</td>
    <td>2022-06-03</td>
  </tr>
  <tr>
    <td>Angelina</td>
    <td>Jolie</td>
    <td>2022-06-04</td>
  </tr>
  <tr>
    <td>Peter</td>
    <td>Parker</td>
    <td>2022-06-05</td>
  </tr>
  <tr>
    <td>Lebron</td>
    <td>James</td>
    <td>2022-06-06</td>
  </tr>
  </tbody>
</table>
 

</body>
</html>