[英]How can I pass date/text to <input type="date">?
I'm trying to set text in input date but nothing shows there.我正在尝试在输入日期中设置文本,但没有显示任何内容。 Is it something wrong with data type?数据类型有问题吗?
<!DOCTYPE html>
<html>
<body>
<table>
<tr>
<td id="invoice">
2020-09-22
</td>
</tr>
</table>
<select id="decisionList">
<option></option>
<option>Payment date</option>
</select>
<div id="choice">
</div>
<script>
document.addEventListener("change", function () {
var decisionList = document.getElementById("decisionList");
var selectedOptionIndex = decisionList.options[decisionList.selectedIndex].index;
var invoiceDate = document.getElementById("invoice");
var invoiceDateText = invoiceDate.textContent;
var finalChoice = document.getElementById("choice");
switch(selectedOptionIndex) {
case 0:
finalChoice.innerHTML='<input type="date">'
break;
case 1:
finalChoice.innerHTML='<input type="date" value="' + invoiceDateText + '">' //here is a problem
break;
default:
finalChoice.innerHTML=''
}
})
</script>
</body>
</html>
I also tried to create new Date based on text I'm passing:我还尝试根据我传递的文本创建新日期:
var newDate = new Date(invoiceDateText);
but also doesn't work.但也不起作用。 As you can see I'm passing 2020-09-22 (from td) so where is the problem?如您所见,我正在通过 2020-09-22(来自 td)那么问题出在哪里?
The variable invoiceDateText contains white spaces and therefore is not parsed correctly.变量 invoiceDateText 包含空格,因此无法正确解析。
You can update that part to the following.您可以将该部分更新为以下内容。
var invoiceDateText = invoiceDate.textContent.trim();
日期应该是一个字符串,格式为YYYY-MM-DD
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.