【发布时间】:2021-02-03 10:48:15
【问题描述】:
我尝试将一列中的日期插入到 .choice 类的日期字段中,但不能这样做。当我返回日期字段长度时,它会显示 10 个标志,所以没关系。日期字段还允许输入 10 个标志。
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? PaymentDate { get; set; }
查看:
<tr>
<td class="payment">
@Html.DisplayFor(modelItem => item.PaymentDate)
</td>
<td>
<select id="decisionList">
<option value="0" selected></option>
<option value="1">None</option>
</select>
</td>
<td class="choice"></td>
</tr>
JS:
document.querySelectorAll("#decisionList").forEach(elem => elem.addEventListener("change", function () {
var selectedOptionIndex = this.options[this.selectedIndex].index;
var invoiceDateText = this.closest('tr').querySelector('.payment').textContent.trim();
var finalChoice = this.closest('tr').querySelector('.choice');
alert(invoiceDateText.length); //returns 10 signs so it's ok
switch (selectedOptionIndex) {
case 0:
finalChoice.innerHTML = '<input type="date">'
break;
case 1:
finalChoice.innerHTML = '<input type="date" value="' + invoiceDateText + '">' // and here doesn't show the date from invoiceDateText
break;
default:
finalChoice.innerHTML = ''
}}));
【问题讨论】:
-
我对你想要做什么感到有点困惑。您是否尝试将日期值传递到日期输入中?我不相信这是可能的,因为这在技术上是一个默认值,并且 html 日期输入不允许默认值。
-
是的,我正在尝试这样做 - 将日期值传递到日期输入中。我使用 jQuery 完成了它,它可以工作,但不适用于 JS ..
-
我必须检查一下,但我认为这在 JS 中是不可能的。我会回复你的。
-
看起来要这样做,它必须是 ISO 格式。