datetimepicker自定义绑定淘汰赛

时间:2014-04-23 14:18:53

标签: knockout.js datetimepicker

我使用https://github.com/xdan/datetimepicker/blob/master/jquery.datetimepicker.js在我的应用程序中使用datetimpicker而不是datepicker我使用了以下绑定

   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);
        }
    }
};

但是这里$ el.datetimepicker(" getDate")返回html元素而不是日期,当我使用datepicker时,它工作正常,但没有日期时间选择器

1 个答案:

答案 0 :(得分:0)

根据文档,要获取您使用的输入字段的值

new Date($el.val())

而不是

$el.datetimepicker("getDate").Value;