2012-01-24 12 views
1

淘汰賽日期選擇器數據綁定可以在這裏找到: jQuery UI datepicker change event not caught by KnockoutJS淘汰賽與jQuery UI的日期選擇器,MM/YY僅

爲了隱藏日期(這是觸發日期選擇器的輸出的唯一的事情),我這樣做對於sans淘汰賽的解決方案:

$(".monthPicker").focus(function() { 
    $(".ui-datepicker-calendar").hide(); 
}); 
$(".monthPicker").datepicker({ 
    dateFormat: 'MM yy, 
    changeMonth: true, 
    changeYear: true, 
    showButtonPanel: true, 
    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).val($.datepicker.formatDate('MM yy', new Date(year, month, 1))); 
    } 
}); 

這是有效的。

但是我怎樣才能使用knockout的數據綁定工作呢?

+1

檢查onClose中的雙引號並將它們更改爲單引號。你可能會使用'data-bind =「.....」東西「.....」得到錯誤'' – BartekR

+0

這是解析,但不綁定到selectedMonthDate,仍然。 –

+0

發佈我正在使用的確切代碼,並保持最新狀態。我試圖找出這個結果,然後結束綁定。如果我毆打別人,我會發布我的答案。 –

回答

0

解決方案是直接更新ViewModel並讓更改通過活頁夾上的更新傳播到datepicker。

<input data-bind="datepicker: monthDate, 
      datepickerOptions: 
      { 
      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(); 
       $root.monthDate(new Date(year, month, 1)); 
      } 
      }" /> 
+0

您可以請您展示您的viewmodel更改嗎?我對Knockout非常陌生,我正在修改別人的代碼,我不知道要改變什麼。 – liamfriel