HTML-5日期字段显示为“mm / dd / yyyy”在Chrome中,即使设置了有效日期 [英] HTML-5 date field shows as "mm/dd/yyyy" in Chrome, even when valid date is set

查看:317
本文介绍了HTML-5日期字段显示为“mm / dd / yyyy”在Chrome中,即使设置了有效日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚将一个ASP.Net MVC应用程序升级到MVC-4。基于DateTime值的输入字段编辑器现在包括HTML-5 type =date属性/值声明。



现在,在Chrome浏览器中查看时,我的日期输入在输入字段中显示为mm / dd / yyyy: //i.stack.imgur.com/Ea0zc.jpgalt =日期字段,在框中输入mm / dd / yyyy>



即使我通过在正确格式的日期中使用值属性:

 < input value =2012/10/02type =日期/> 

我仍然在输入框中输入mm / dd / yyyy,直到用户手动更改值。



这个问题似乎与Chrome有关,并且独立于我的后端框架。在行动中看到这个问题: jsFiddle



当然,编辑记录是个大问题。如果用户提取已有有效日期的记录,那么它将不会通过提交验证,除非他/她单击该字段并手动重置值。



其他浏览器没有问题。



这是一个Chrome错误吗?或者我错过了关于HTML-5日期字段的工作方式?



更新

看到这个修改小提琴: http://jsfiddle.net/HudMe/5/ 它有包括HTML-4和HTML-5日期输入,每个输入都将10/01/2012设置为页面加载时的值。



点击进入任一日期领域。
Javascript应该使用该元素的字段值来提示警报。


因为有效日期已经通过value属性传入,所以应该显示2012年10月1日,但在Chrome中,对于HTML-5日期字段,什么也没有显示手动重置此值,然后再次单击,现在将显示。



在Safari,Firefox,IE和Opera的页面加载之后,HTML5字段的值显示并按预期进行警报,无需调整。



关于接受的答案的注释

对于Asp.net mvc-4的其他用户,您可以使用 [DisplayFormat ]您的视图模型中的DateTime字段声明中的属性。 (可在 https://stackoverflow.com/a/12634470/613004 找到)


<在chrome中设置你需要做的值 YYYY-MM-DD 我猜,因为这工作: http://jsfiddle.net/HudMe/6/

因此,要使其工作,您需要将日期设置为 2012-10-01


I just upgraded an ASP.Net MVC application to MVC-4. The field editor for inputs based on DateTime values now include's the HTML-5 type="date" attribute/value declaration.

Now, when viewing in Chrome, my date inputs show up with "mm/dd/yyyy" in the input field:

Even when I pass in a correctly-formatted date with the value attribute:

<input value="2012/10/02" type="date"/>

I'm still getting "mm/dd/yyyy" in the input box, which is there until the user manually changes the value.

The problem appears to be with Chrome, and is independent of my back-end framework. See this problem in action: jsFiddle

...big problem for editing records, of course. If the user pulls up a record that already has a valid date, it won't pass validation on submit, unless s/he clicked into the field and reset the value manually.

No problems with the other browsers.

Is this a Chrome bug? Or have I missed something about the way the HTML-5 date field is supposed to work?

UPDATE
See this revise fiddle: http://jsfiddle.net/HudMe/5/ It has both an HTML-4 and an HTML-5 date input, each with "10/01/2012" set as the value on page-load.

Click into either date field. The Javascript should pup up an alert with the value of the field for that element.

Because a valid date has been passed in with the value attribute, this should show "10/01/2012", but in Chrome, for the HTML-5 date field, nothing shows. Reset this value by hand, then click again, and it will now show.

The value from the HTML5 field shows and alerts as expected without adjustment after pageload in Safari, Firefox, IE and Opera.

Note on accepted answer:
For other users of Asp.net mvc-4, you can adjust the display format with the [DisplayFormat] attribute on the DateTime field declaration in your view-model. (found at https://stackoverflow.com/a/12634470/613004 )

解决方案

In chrome to set the value you need to do YYYY-MM-DD i guess because this worked : http://jsfiddle.net/HudMe/6/

So to make it work you need to set the date as 2012-10-01

这篇关于HTML-5日期字段显示为“mm / dd / yyyy”在Chrome中,即使设置了有效日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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