我试图在单击按钮时过滤开始日期和结束日期之间的表行,但我不知道我错在哪里,我只是从这里得到了这个片段,但它不适合我。
我是JS的新手,我试图在代码中实现这部分,但它没有,我无法找到出了什么问题,我的html输入日历是mm/dd/yyyy格式,我的表日期来自yyyy-mm-dd格式的api。提前感谢
function searchDate() {
var input_startDate, input_stopDate, table, tr, i;
// get the values and convert to date
input_startDate = new Date(document.getElementById("from_date").value);
input_stopDate = new Date(document.getElementById("To_date").value);
table = document.getElementById("table_id");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
// you need to get the text and convert to date
let td_date = new Date(tr[i].getElementsByTagName("td").textContent);
// now you can compare dates correctly
if (td_date) {
if (td_date >= input_startDate && td_date <= input_stopDate) {
// show the row by setting the display property
tr[i].style.display = 'table-row;';
} else {
// hide the row by setting the display property
tr[i].style.display = 'none';
}
}
}
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<div style="margin-left: 19px; font-size: 17px;">NetDue Date</div>
<div class="row" id="id_row">
<div class='row' style="margin-left: 19px;"> From:</div>
<input type="date" name="from_Date" style="margin-left: 30px; font-size: 20px;" placeholder="From_Date" id="From_Date" required="">
<div class='row' style="margin-left: 19px;"> To:</div>
<input type="date" name="to_Date" style="margin-left: 30px;font-size: 20px;" placeholder="To_Date" id="To_Date" required="">
<input type="submit" value="Apply" id="btn_submit" style=" width:90px;
height:45px; margin-left: 40px; margin-bottom: 30px;" class="btn btn-success btn" onclick="searchDate()">
<input type="reset" value="Reset" id="btn_submit" style=" width:90px;
height:45px;margin-left: 65px; margin-bottom: 30px;" class="btn btn-success btn">
</div>
<table class="table table-striped border datatable" id='table_id'>
<!-- <table class="dataclass" id='table_id'> -->
<thead>
<thead id="tablehead">
<tr>
<th> Region</th>
<th> Area </th>
<th> Date </th>
</tr>
<tr>
<td> US</td>
<td> NorthAmerica</td>
<td> 2021-27-10
<td>
</tr>
<tr>
<td> US</td>
<td> NorthAmerica</td>
<td> 2021-26-10
<td>
</tr>
<tbody>
</table>
</div>
</div>
</div>
</div>
请查看以下HTML和JS代码:我希望这将满足您的期望
<JS代码/strong>
function searchDate() {
var input_startDate, input_stopDate, tr, i;
// get the values and convert to date
input_startDate = new Date(document.getElementById("From_Date").value);
input_stopDate = new Date(document.getElementById("To_Date").value);
tr = document.querySelectorAll("#table_id tbody tr");
for (let i = 0; i < tr.length; i++) {
// ensure we have a relevant td
let td = tr[i].getElementsByTagName("td");
if (!td || !td[2]) continue;
// you need to get the text and convert to date
let td_date = new Date(td[2].textContent);
// now you can compare dates correctly
if (td_date) {
if (td_date >= input_startDate && td_date <= input_stopDate) {
// show the row by setting the display property
tr[i].style.display = 'table-row;';
} else {
// hide the row by setting the display property
tr[i].style.display = 'none';
}
}
}
}