如何修复 jQuery datepicker 的区域设置,使其在 Firefox 和 IE7 中工作? [英] How to fix regional settings for jQuery datepicker so it works in Firefox and IE7?

查看:11
本文介绍了如何修复 jQuery datepicker 的区域设置,使其在 Firefox 和 IE7 中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jQuery 的日期选择器和 asp.net MVC4.日期选择器在 Firefox 中工作,但在 IE7 中,我通过 asp.net 的验证收到消息,该字段不是日期.

I am using jQuery' s datepicker and asp.net MVC4. The datepicker works in Firefox but in IE7 i get the message through the asp.net's validation that the field is not a date.

这是日期选择器的代码

if (!Modernizr.inputtypes.date) {
        $(function() {
            $.datepicker.setDefaults($.datepicker.regional['en-GB']);
            $(".datefield").datepicker();
        });
    }

这是我在 Web.config 中的全球化设置

This is my globalization setting in Web.config

<全球化 uiCulture="en-GB"culture="en-GB"/>

例如在 Firefox 中,日期显示为19/03/2012"字符串并被 asp.net 的验证设置(客户端和服务器端)接受.在 IE7 中,客户端不接受相同的日期字符串.如果我将其更改为2012 年 3 月 19 日",客户端接受该日期,但服务器会抛出异常 - InvalidOperationException.Nullable 对象必须有一个值."

E.g. in Firefox the date is shown as "19/03/2012" string and accepted by the asp.net's validation setup (client and server side). In IE7 the same date string is not accepted on the client. If i change it to "03/19/2012" the client accepts the date but then the server throws an exception - "InvalidOperationException. Nullable object must have a value."

我的 viewModel 使用了一个可以为 null 的 DateTime,我在控制器 post 操作中将它转换为一个不可为 null 的 DateTime.这在 Firefox 中有效,但在 IE7 中,viewModel 中的日期值为空.有什么问题?

My viewModel uses a null-able DateTime that i cast to a non null-able DateTime in the controllers post action. This works in Firefox but in IE7 the value for the date from the viewModel is null. What is the problem?

推荐答案

下面这行什么都不做:

$.datepicker.setDefaults($.datepicker.regional['en-GB']);

如果您不包含默认不包含的相应的语言文件在 ASP.NET MVC 4 模板中.

if you don't include the corresponding language file which is not included by default in the ASP.NET MVC 4 template.

您可以尝试明确设置格式:

You may try setting the format explicitly:

$.datepicker.setDefaults({ dateFormat: 'dd/mm/yy' });

但这仅涉及在日期选择器中选择日期后应如何格式化日期.它与验证无关.

But this only concerns how the date should be formatted after selecting it in the datepicker. It has nothing to do with validation.

客户端验证由 jquery.validate 插件执行,该插件反过来使用浏览器当前配置的文化(这可能解释了您在 FF 和 IE 之间观察到的差异,例如一个可能配置为使用 en-GB 和其他 en-US) 或 ISO 日期.

The client side validation is performed by the jquery.validate plugin which in turn uses either the browser currently configured culture (which might explain the discrepancies you are observing between FF and IE, for example one might be configured to use en-GB and the other en-US) or ISO dates.

您可以覆盖此自定义验证并使其使用您的自定义格式以确保这将跨浏览器工作:

You could override this custom validation and make it use your custom format to ensure that this will work cross browser:

if (!Modernizr.inputtypes.date) {
    $(function () {
        $.datepicker.setDefaults({ dateFormat: 'dd/mm/yy' });
        $('.datefield').datepicker();
    });

    jQuery.validator.addMethod(
        'date',
        function (value, element, params) {
            if (this.optional(element)) {
                return true;
            };
            var result = false;
            try {
                $.datepicker.parseDate('dd/mm/yy', value);
                result = true;
            } catch (err) {
                result = false;
            }
            return result;
        },
        ''
    );
}

这篇关于如何修复 jQuery datepicker 的区域设置,使其在 Firefox 和 IE7 中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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