JQuery Datepicker 不会发布英国日期字符串 [英] JQuery Datepicker Will not post with UK date string

查看:14
本文介绍了JQuery Datepicker 不会发布英国日期字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果问题很明显,但我不知道为什么它突然不起作用,我深表歉意.我有一个 jquery 日期选择器,只要我记得,它就一直工作正常,但是突然之间,当我尝试提交日期选择器在日期选择器上的表单时,它再次出现,好像我提交的日期无效.我已经使用以下代码行将我的日期设置为英国风格:

Apologies for if the question is obvious but I can't figure out why it is suddenly not working. I have a jquery datepicker that has been working fine as long as I can remember, but all of a sudden, when I try to submit the form that the datepicker is on the datepicker reappears as though the date I am submitting is invalid. I have already set my date to uk style using the following line of code:

    <script type="text/javascript">
    $(document).ready(function () {
        $('.date').datepicker({ dateFormat: "dd/mm/yy", minDate: 0 });

    }); 
    </script>

我在我的视图中设置了日期选择器,如下所示:

And I setup the datepicker in my view like this:

@Html.TextBox("DateDue", "", new { @class = "date" })

我认为有些东西要求选择的日期字符串采用美国格式(因为它会提交有效的美国日期 (mm/dd/yyyy) 而不是英国格式,即使它已在 javascript 中设置.

I think that something is requiring the datestring selected to be in US format (as it will submit a valid us date (mm/dd/yyyy) but not the UK format, even though it has been set in the javascript.

我是否遗漏了一些明显的东西?谁能告诉我我在这里做错了什么?

Am I missing something obvious? Could anyone tell me what I'm doing wrong here?

我不确定是否有必要,但日期选择器所在的表单是这样创建的:

Am not sure if it is necessary but the form that the datepicker is on is created like this:

@using (
    Html.BeginForm(
        Html.BeginForm(
            "Create",
            "Task",
            FormMethod.Post,
            new { id = "taskCreateForm" }
        )
    )
)

附注.日期字符串在 Firefox 中实际上可以正常工作和发布,但在 Chrome 中没有问题

PS. The datestring actually works and posts without a problem in Firefox but not in Chrome

感谢任何帮助

当我禁用不显眼的 js 验证时问题就消失了:

The problem goes away when I disable the unobtrusive js validation:

<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"
        type="text/javascript"></script>

我感觉这与此有关,并尝试从模型注释中移除 [Required] 和 [DataType(DataType.Date)],但这并没有奏效.

I had a feeling it was something to do with this and tried remoing the [Required] and [DataType(DataType.Date)] from the model annotation but this didn't do the trick.

不确定如何保留不显眼的 js 以及如何让英国日期字符串正常工作,但我越来越多地转向编写我自己的 javascript 表单验证,而不是打扰内置的东西,这似乎从来没有适用于比必需"验证检查更复杂的任何事情..

Not sure how to leave the unobtrusive js in as well as have the uk date string working ok, but am more and more turning to write my own javascript validation for forms than to even bother with the built in stuff, which never seems to work for anything more complicated than a 'required' validation check..

推荐答案

谷歌搜索提供了很多与本地化/全球化 asp.net mvc 3 验证相关的结果.

a google search gave a lot of results relating to localizing/globalizing asp.net mvc 3 validation.

这篇文章可能特别关注:

this article in particular might be on to something:

http://www.hanselman.com/blog/GlobalizationInternationalizationAndLocalizationInASPNETMVC3JavaScriptAndJQueryPart1.aspx

向下滚动到:全球化的 jQuery 非侵入式验证"

scroll down to: "Globalized jQuery Unobtrusive Validation"

这个问题的答案也可能有所帮助:使用 asp.net mvc 3 本地化 jquery 验证

the answer to this question might help as well: Localizing jquery validation with asp.net mvc 3

jQuery 允许您像这样覆盖不同的验证功能:

jQuery allows you to override the different validation functions like so:

 $.validator.methods.number = function (value, element) {
     return !isNaN($.parseFloat(value));
 }

您可以对日期验证器执行类似操作,并针对有效英国日期的 regEx 运行它.

you could do something like this for the date validator, and run it against a regEx for a valid UK date.

这篇关于JQuery Datepicker 不会发布英国日期字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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