【问题标题】:jQuery datepicker displays different on Chrome and on FirefoxjQuery datepicker 在 Chrome 和 Firefox 上显示不同
【发布时间】:2013-02-03 02:08:38
【问题描述】:

在一个 MVC3 应用程序中,我使用了一个 jQuery datepicker 插件,它在 Firefox 和 Chrome 中的显示完全不同。应用中有相关代码和两张截图:

<head>
    <link href="@Url.Content("~/Content/themes/redmond/jquery-ui-1.8.18.custom.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-ui-1.8.18.custom.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.datepicker.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.datepicker-fr.js")" type="text/javascript"></script>
    <script>
        $(document).ready(function () {
            $('.datefield').datepicker({
                showOn: 'both',
                buttonImage: "/content/images/calendar.gif",
                showAnim: "clip",
                regional: "fr",
                defaultDate: null,
                changeMonth: true,
                changeYear: true
            });
        });
    </script>
</head>

@using(...)
{
    ...
    @Html.LabelFor(model => model.candidat.date_naissance)
    @Html.EditorFor(model => model.candidat.date_naissance, new { @class = "date" })
    ...
}

我使用 EditorTemplate:

@model DateTime
@Html.TextBox("", String.Format("{0:d}", Model.ToShortDateString()),
  new { @class = "datefield", type = "date" })

在模型中,字段candidat.date_naissance为:

[Required(ErrorMessageResourceType = typeof(CandidatResources), ErrorMessageResourceName = "RequiredDateNaissanceMessage")]
[Display(ResourceType = typeof(CandidatResources), Name = "DateNaissanceDisplay")]
[DataType(DataType.Date, ErrorMessageResourceType = typeof(CandidatResources), ErrorMessageResourceName = "InvalidDateNaissanceMessage")]
public System.DateTime date_naissance { get; set; }

生成的 HTML 是:

  • 在 Firefox 中:

    输入类型="日期" 值="12/19/1974" 名称="candidat.date_naissance" id="candidat_date_naissance" data-val-required="La date de naissance est obligatoire" data-val="true" class= “日期字段有日期选择器”
  • 在 Chrome 中: 输入 class="datefield hasDatepicker" data-val="true" data-val-required="La date de naissance est obligatoire" id="candidat_date_naissance" name="candidat.date_naissance" type="date" value="12/ 19/1974"

最后,这是相同数据的截图:

火狐:

铬:

在 Chrome 中,字符串“mm/dd/yyyy”始终显示,即使已经存在日期(例如,在输入日期并保存在 Firefox 中之后,我希望在刷新时也能在 Chrome 中看到它,但是这不会发生)。

对这种行为的解释是什么以及如何解决?

我已经尝试过this solution,但无济于事。

注意: 这发生在 Cassini 服务器和 IIS(本地和虚拟盒子中)上。

【问题讨论】:

    标签: google-chrome jquery-ui-datepicker


    【解决方案1】:

    请参考以下文章。 如果您想在值属性或表单提交中应用 mm/dd/yyyy 格式,则不应使用 type=date。

    【讨论】:

    • 感谢您提供的这条信息,它让我立即摆脱了麻烦。谢谢一百万。
    • 这个引起了我的注意,因为我使用的是 jQuery validation plug-in,它使用像 type="date" 这样的标记来控制验证。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 2018-05-31
    相关资源
    最近更新 更多