Jquery 日期选择器 Chrome [英] Jquery Datepicker Chrome

查看:46
本文介绍了Jquery 日期选择器 Chrome的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 jQuery UI Datepicker 时,我们在 Google Chrome 中使用时遇到了一个问题:当我们输入一天大于 12 的日期时,它不接受它作为有效日期,这是因为 chrome 认为日期格式是 mm/dd/yyyy.我们尝试通过添加代码来尝试将日期设置强制为 dd/mm/yyyy 来解决此问题

When using jQuery UI Datepicker, we encouter a problem when used in Google Chrome: when we enter a date with a day higher than 12, it does not accept it as a valid date, this is because chrome thinks the dateformat is mm/dd/yyyy. We tried to solve this by adding code to try to force the date settings into dd/mm/yyyy

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

有没有办法解决这个问题,所以我们的日期选择器会接受 dd/mm/yyyy 值?我们只有谷歌浏览器有这个问题,datefix 适用于 Firefox,即 &苹果浏览器.我们正在使用 ASPX &MVC3 与这个项目.

Is there any way to resolve this issue, so our datepicker will accept dd/mm/yyyy values? We only have this issue in google Chrome, the datefix works for firefox, ie & safari. We are using ASPX & MVC3 with this project.

如果有人能解决我们的问题,那就太好了

If someone could solve our issue, that would be great

谢谢

推荐答案

我遇到了同样的问题,并且与所有基于 Webkit 的 Web 浏览器有关.如果您设置大写 M,则文本框会显示带有字母的飞蛾.对我来说最好的解决方案是覆盖 jquery.validate.js 中的验证日期函数

I have had the same problem and is related with all Webkit based web browsers. If you set uppercase M the textbox show the moth with letters. The best solution for me was to override the validate date function from jquery.validate.js

创建 jquery.validate.date.js 并确保它在 jquery.validate.js 之后加载

Create jquery.validate.date.js and ensure it is loaded after jquery.validate.js

在 jquery.validate.date.js 中添加以下代码

Add the following code to jquery.validate.date.js

$(function() {
    $.validator.methods.date = function (value, element) {
        if ($.browser.webkit) {

            //ES - Chrome does not use the locale when new Date objects instantiated:
            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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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