javascript - 无法在输入类型日期中设置日期
问题描述
我知道,以前有人问过类似的问题,但我不明白答案/我觉得它不能解决我的问题。我无法在我的输入类型日期中设置日期。
export default function NewNoteModal() {
return(
<div id = "newNoteModal">
{minimumSetDate()}
<div id = "newNote-header">
<h2>NoteKeeper</h2>
</div>
<form id = "newNote-content">
<label htmlFor = "noteName">Note Name:</label>
<input type="text" id = "noteName"></input>
<br></br>
{/* ---------------------- */}
<label htmlFor="checkB-important">Important:</label>
<input type="checkbox" name="important" value="important" id = "checkB-important"></input>
<br></br>
{/* ---------------------- */}
<label htmlFor="remindDate">Remind me on:</label>
<input type="date" name="remindDate" id = "remindDate"></input>
<br></br>
{/* ---------------------- */}
</form>
</div>
);
}
function minimumSetDate(){
let today = new Date();
let day = today.getDate(); // typeof = number
let month = ('0' + (today.getMonth()+1)).slice(-2); // typeof = number
let year = today.getFullYear(); // typeof = number
let currentDate = `${year}-${month}-${day}`; // typeof = string;
console.log(typeof(currentDate), currentDate);
document.getElementById("remindDate").value = currentDate;
}
控制台给出“TypeError:无法设置属性'value' of null”;
但是,这在 w3schools 中有效
document.getElementById("myDate").value = "2014-02-09";
解决方案
let today = new Date();
let day = today.getDate(); // typeof = number
let month = ('0' + (today.getMonth()+1)).slice(-2) // typeof = number
let year = today.getFullYear(); // typeof = number
let currentDate = `${year}-${month}-${day}`; // typeof = string;
document.getElementById("remindDate").value = currentDate;
<input type="date" id="remindDate">
推荐阅读
- javascript - 如何在 ngFor 循环中使用 ngb-accordion?
- ajax - Woocommerce 使用 ajax 对购物车的总价应用折扣
- laravel - 如何在 Laravel 7 中运行普通查询,而不是使用 Eloquent
- java - Palindrome 程序适用于单个测试用例,但在其他情况下继续输入
- c++11 - 如何删除文件并避免引入竞争条件?
- html - 组合 2 个 html 文件时,html 中的多个脚本标签不起作用
- c# - 我需要检查整数中的每个数字在c#中是偶数还是奇数,返回偶数或奇数
- image - 由于ocr的行删除,填补了汉字的空白
- asp.net-core - 将 WebApi 托管为 IIS 应用程序,以便它运行启动类
- java - 如何将重写规则添加到 Spring Boot 2.3.1