【问题标题】:The Jquery-ui datepicker custom display "the field must be a valid date"jquery-ui datepicker自定义显示“该字段必须是有效日期”
【发布时间】:2012-04-12 08:25:17
【问题描述】:

当我使用带有日期选择器自定义的必填字段时遇到问题。 如果我单击下一步按钮,字段自定义决策日期显示“该字段必须是有效日期”。 如果我像这样更改 datepicker 的格式日期:“dateFormat: 'd/m/yy'” 它可以工作,但我只想显示月份和年份而不是日期。 感谢您的帮助。

我的看法是:

    <script>
        $(document).ready(function () {
            $(".DatepickerMonthYears").datepicker({
                    changeMonth: true,
                changeYear: true,
                showButtonPanel: true,
                dateFormat: 'm/yy',
                required: false,
                onClose: function(dateText, inst) { 
                    var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
                    var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
                    $(this).datepicker('setDate', new Date(year, month, 1));
                }
            });


            $(".DatepickerMonthYears").focus(function () {
                $(".ui-datepicker-calendar").hide();
                $("#ui-datepicker-div").position({
                    my: "center top",
                    at: "center bottom",
                    of: $(this)
                });
            });
            });
    </script>

@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { id = "FormAction" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Create </legend>
<div id="tabs">
    <ul>
        <li><a href="#tabs-1">Step1</a></li>

    </ul>
    <div id="tabs-1">
        @Html.Partial("Step1", Model)
    </div>

    <div class="buttonNextPrevious">
        <a id="previous" class="linkLikeButton" href="#">Previous Step</a>
        <a id="next" class="linkLikeButton" href="#">Next Step</a>
    </div>
</div>


</fieldset>    
}

我的部分观点是:

@model SiteWebEmpty.Models.ArticleRequest.CreateArticle.DisplayCreateAllStep

<fieldset>
<legend>Step 1</legend>
                @Html.LabelFor(model => model.Step1.CustomerDecisionDate, new { @class = "LabelStep1" })
                @Html.TextBoxFor(step1 => step1.Step1.CustomerDecisionDate, new { @class = "DatepickerMonthYears" })
                @Html.ValidationMessageFor(model => model.Step1.CustomerDecisionDate)
                <br />
</fieldset>

我的模型是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using SiteWebEmpty.Models.AttributValidation;


namespace SiteWebEmpty.Models.Step1
{
    public class Step1
    {
        [Display(Name = "Customer Decision Date")]
        public DateTime CustomerDecisionDate { get; set; }

    }
}

更新

此链接上的解决方案: jQuery UI DatePicker to show month year only 与“Erik Polder”的帖子一起工作,但如果我将格式日期“yy-mm”更改为“mm-/yy”,我会遇到同样的错误:/

更新2 我找到了解决方案,我添加了一个脚本以便它工作 脚本:jquery.ui.datepicker-fr.js

【问题讨论】:

  • 我对这篇文章中的代码有同样的问题,但感谢您的帮助:)。我更新了我的帖子:)
  • 我找到了解决方案 :) 我更新了我的帖子 :)。
  • 这很好,而不是更新问题,您可以回答自己的问题,以便将来有人在寻找他应该知道解决方案

标签: jquery asp.net-mvc asp.net-mvc-3 jquery-ui


【解决方案1】:

试试……

$('.DatepickerMonthYears').datepicker( {
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        dateFormat: 'MM yy',
        onClose: function(dateText, inst) { 
            var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
            var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
            $(this).datepicker('setDate', new Date(year, month, 1));
        }
    });

【讨论】:

    猜你喜欢
    • 2011-12-01
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 2012-04-27
    相关资源
    最近更新 更多