继续 knockout js bind with datetimepicker gives an exception 我现在能够使用datetimepicker与knockout,但是我无法使用同一工具的时间选择器选项,我试过的代码嵌入到下面的jsfiddle但是抛出错误
<code>
http://jsfiddle.net/saqibshakil/scdET/
</code>
编辑后检查控制台
答案 0 :(得分:3)
在getDate
上调用timepicker
似乎未返回实际的Date
。
您似乎可以成功使用datetimepicker
来呼叫它。所以,你的绑定看起来像:
ko.bindingHandlers.timepicker = {
init: function (element, valueAccessor, allBindingsAccessor) {
//initialize timepicker with some optional options
var options = allBindingsAccessor().timepickerOptions || {};
$(element).timepicker(options);
//handle the field changing
ko.utils.registerEventHandler(element, "change", function () {
var observable = valueAccessor();
observable($(element).datetimepicker("getDate"));
});
//handle disposal (if KO removes by the template binding)
ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
$(element).timepicker("destroy");
});
},
//update the control when the view model changes
update: function (element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor()),
current = $(element).datetimepicker("getDate");
if (value - current !== 0) {
$(element).datetimepicker("setDate", value);
}
}
};