如何解决区域设置的jQuery日期选择器,因此在Firefox和IE7? [英] How to fix regional settings for jQuery datepicker so it works in Firefox and IE7?

查看:83
本文介绍了如何解决区域设置的jQuery日期选择器,因此在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.

这是code的日期选择器

This is the code for the datepicker

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文化=EN-GB/>

例如。在Firefox中的日期显示为19/03/2012的字符串,并由asp.net的验证设置(客户端和服务器端)所接受。在IE7相同的日期字符串是不能接受的客户端上。如果我把它改为2012年3月19日的客户端接受的日期,但那么服务器会抛出异常 - 。InvalidOperationException异常可空对象必须有一个值

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."

我的视图模型使用空日期时间能够让我投来一个非空的DateTime能在控制器后的行动。这在Firefox,但在IE7从视图模型的日期值为null。有什么问题?

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?

推荐答案

下面这行不执行任何操作:

The following line does nothing:

$.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.

客户端验证是由反过来使用任何浏览器的当前配置的文化(这或许可以解释你是FF之间观察不符的 jquery.validate 插件进行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日期选择器,因此在Firefox和IE7?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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