【发布时间】:2021-08-04 13:37:58
【问题描述】:
我使用 window.location.href 作为元素过滤器。但是,当我单击过滤器按钮时,并未显示所有元素。我操作的不显示,直接从数据库返回的有。
首先,按钮是这样的,每个月1个:
<div class="monthsButton">
<button type="button" data-month="0" >2021</button>
<button type="button" data-month="1" >Janeiro</button>
<button type="button" data-month="2" >Fevereiro</button>
其次,要将日期用作过滤器,我使用以下内容:
const month = req.query.month;
if(month > 0) {
launch = lancamentos.filter(item => {
const data = new Date(item.data);
return (data.getMonth() + 1) == month
})
}
最后,要执行操作,按钮配置如下:
const monthsButton = document.querySelectorAll('.monthsButton button')
for (let button of monthsButton) {
const month = button.dataset.month
button.onclick = () => {
window.location.href= `?month=${month}`
}
}
问题是过滤器按钮识别日期,使用它,但过滤时不显示,日期和数值也不显示。
【问题讨论】:
-
您可能希望将
const month = req.query.month;设为数字而不是字符串。我仍然不知道你的实际问题是什么。从你的问题很难确定你的问题是什么。究竟是什么不正常? -
返回一个数字。
-
返回一个字符串格式的数字。我也不知道你的问题是什么。
-
好的,我修改它返回一个数字,但错误仍然存在
-
我能问一下为什么你得到一个日期对象,把它改成一个字符串,把它分成几部分,再做一个新的日期?为什么你不只是使用第一个日期对象的 getMonth ???
data.getMonth()应该与LaunchUtils.formatDate(dataString).getMonth()没有区别
标签: javascript node.js ejs window.location