将HTML 5 DatePicker与Date输入字段一起使用 [英] Using the HTML 5 DatePicker with the Date input field

查看:61
本文介绍了将HTML 5 DatePicker与Date输入字段一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解有关HTML5日期的更多信息.我已将以下代码添加到MVC Core 2.1视图中:

I am trying to learn more about HTML5 dates. I have added the following code to an MVC Core 2.1 view:

<label asp-for="DOB" class="control-label"></label>

@{ 
    var minDate = DateTime.Now.AddYears(-99).Date;
    var maxDate = DateTime.Now.AddYears(-18).Date;
    var defaultDate = DateTime.Now.AddYears(-25).Date;
}

<input asp-for="DOB" class="datefield" type="date" min=@minDate max=@maxDate value=@defaultDate/>
<span asp-validation-for="DOB" class="text-danger"></span>

呈现的HTML如下:

<input class="datefield" type="date" min="23/10/1919 00:00:00" max="23/10/2000 00:00:00" value="23/10/1993 00:00:00" data-val="true" data-val-required="The DOB field is required." id="DOB" name="DOB" />

我有几个问题:

1)日期的默认值未显示,即:dd/mm/yy.如果我将MVC代码更改为此,那么它将起作用:

1) The default value of the date does not appear i.e. it says: dd/mm/yy. If I change my MVC code to this then it does work:

<input asp-for="DOB" class="datefield" type="date" min=@minDate max=@maxDate value="1900-01-01"/>

为什么不接受专门用于默认值的变量?

Why is it not accepting the variable specifically for the default value?

2)在MVC视图中将类更改为:datefield(来自:form-control)是否有任何暗示.

2) Are there any implications of changing the class to: datefield (from: form-control) in the MVC view.

3)是否可以为日历指定默认日期,而在输入字段中不显示该值?

3) Is it possible to specify a default date for the calendar, without the value appearing in the input field?

推荐答案

设置date-input的值必须采用yyyy-mm-dd格式. https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/input/date#Value

Setting a value of date-input must be in yyyy-mm-dd format. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date#Value

这篇关于将HTML 5 DatePicker与Date输入字段一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆