【问题标题】:Insert dates into input type=date not possible将日期插入输入类型 = 日期不可能
【发布时间】: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 格式。

标签: javascript asp.net-mvc


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2018-10-27
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    相关资源
    最近更新 更多