使用Jquery UI datepicker敲除,仅限MM / YY

可以在这里找到一个淘汰日期选择器数据绑定器: 未被KnockoutJS捕获的jQuery UI datepicker更改事件

为了隐藏日期(这是唯一触发datepicker输出的东西),我为sans knockout解决方案做了这个:

$(".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的数据绑定器呢?

解决方案是直接更新ViewModel并让更改通过绑定器上的更新传播到datepicker。