datetimpicker custom binding knockout

十年热恋 提交于 2021-01-29 16:12:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!