我正在做一个简单的 MVC4 Internet 应用程序,它允许向类别添加一些项目。
这是我到目前为止所做的事情。
我在 mvc View 中有一个日期选择器。日期选择器的脚本是这样的。
<script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.20.min.js")" type="text/javascript"></script>
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$(function () {
$('#dtItemDueDate').datepicker({
dateFormat: 'dd/mm/yy',
minDate: 0
});
});
</script>
我的模型属性:
[DisplayName("Item DueDate")]
[Required]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}",ApplyFormatInEditMode = true)]
[DataType(DataType.DateTime)]
public DateTime? dtItemDueDate { get; set; }
public char charCompleted { get; set; }
在我看来,我已经这样做了:
@Html.TextBoxFor(m => m.dtItemDueDate)
@Html.ValidationMessageFor(m => m.dtItemDueDate)
错误是这样的:
The field Item DueDate must be a date.
奇怪的是,它在 IE 和 mozilla 中可以工作,但在 Chrome 中不起作用。
我在 SO 上找到了很多帖子,但没有一个有帮助
有什么想法/建议吗?
最佳答案
在不更改 jquery.validate.js 的情况下,您可以在 $(document).ready()
中使用下面的代码片段:
jQuery.validator.methods.date = function (value, element) {
var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
if (isChrome) {
var d = new Date();
return this.optional(element) || !/Invalid|NaN/.test(new Date(d.toLocaleDateString(value)));
} else {
return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
}
};
关于jquery - 字段日期必须是chrome中mvc中的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15706455/