问题
i am using datetimpicker not datepicker in my application using https://github.com/xdan/datetimepicker/blob/master/jquery.datetimepicker.js i used following binding
ko.bindingHandlers.datetimepicker = {
init: function (element, valueAccessor, allBindingsAccessor) {
var $el = $(element);
//initialize datepicker with some optional options
var options = allBindingsAccessor().datepickerOptions || {};
$el.datetimepicker(options);
//handle the field changing
ko.utils.registerEventHandler(element, "change", function () {
var observable = valueAccessor();
var $el = $(element);
observable($el.datetimepicker("getDate").Value);
});
//handle disposal (if KO removes by the template binding)
ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
$el.datetimepicker("destroy");
});
},
update: function (element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor()),
$el = $(element),
current = $el.datetimepicker("getDate").Value;
if (value - current !== 0) {
$el.datetimepicker("setDate", value);
console.log("just set", $el.datetimepicker("getDate").Value);
}
}
};
but here $el.datetimepicker("getDate") returns html element not date when i use datepicker it works fine but not with date time picker
回答1:
According to the documentation, to get the value of the input field you use
new Date($el.val())
instead of
$el.datetimepicker("getDate").Value;
来源:https://stackoverflow.com/questions/23247170/datetimpicker-custom-binding-knockout