在输入类型中插入日期=日期不可能



我试图将一列中的日期插入class.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 = ''
}}));

我找到了问题的答案。问题是我在View中使用了html助手,并试图在html助手代码中插入简单的输入。改变的关键是:

finalChoice.innerHTML = '<input class="form-control datepicker" id="item_PaymentDate" name="item.PaymentDate" type="date" value="' + invoiceDateText + '">'

当按下F12并检查您的网站代码时,您可以随时检查html帮助程序代码的外观

最新更新