2016-11-09 52 views
1

在MVC中,我使用共享文件夾中的編輯器模板來激活datepicker。 所以我的Date.cshtml看起來像這樣;MVC JQuery Datepicker無法在jQuery隱藏並顯示日期字段後觸發

@model DateTime? 
@Html.TextBox("", Model.HasValue ? Model.Value.ToLongDateString() : string.Empty, 
    new { @class = "datefield", type = "date", style = "width:200px;" }) 

在我看來,我有;

<td class="rightCell"> 
    @Html.EditorFor(model => model.Assessment.SsipAccreditationDate) 
    @Html.ValidationMessageFor(
        model => model.Assessment.SsipAccreditationDate, "*") 
</td> 

此字段在視圖模型定義爲

[DataType(DataType.Date)] 
    [DisplayName("SSIP Accreditation Date")] 
    public DateTime? SsipAccreditationDate { get; set; } 

以及連接最多的就是這個腳本;

var initialiseDate = function() { 
    $(".datefield").datepicker({ 
     showOn: "both", 
     dateFormat: "dd MM yy", 
     changeMonth: true, 
     changeYear: true 
    }).next("button.ui-datepicker-trigger") 
      .attr("tabIndex", "-1"); 
} 

而問題是隻有在日期已存在的日期選擇器彈出。如果該字段爲空,則不會彈出。

在ie開發人員工具中,該字段的標記如下所示;

<input name="Assessment.SsipAccreditationDate" class="datefield hasDatepicker" id="Assessment_SsipAccreditationDate" style="width: 200px;" type="date" value="" data-val="true" data-val-date="The field SSIP Accreditation Date must be a date."> 
<button tabindex="-1" class="ui-datepicker-trigger" type="button">...</button> 
<span class="field-validation-valid" data-valmsg-replace="false" data-valmsg-for="Assessment.SsipAccreditationDate">*</span> 

那麼是怎麼回事?

編輯 - 問題的關鍵部分是,在jQuery中我顯示和隱藏日期,這似乎有所作爲。在另一頁上,我沒有這個日期字段的問題。

回答

0

嘗試

如果當前值爲null設定

$("#datefieldid").val(''); 

這將改變的東西時,datapicket不喜歡空我猜

+0

嗨周華健,我已經編輯了一個問題,我意識到在這種情況下發生的關鍵因素是,在jQuery中,我隱藏並顯示日期字段,這會產生變化,但我不知道爲什麼。當我顯示日期字段時,我再次調用initialiseDate函數以確保datepicker的工作,但它不。 – arame3333