Using Knockout.js how do bind a Date property to a HTML5 date picker?

后端 未结 8 677
南旧
南旧 2021-01-04 00:37

(this only works in Chrome at the moment as most browsers don\'t yet implement date picker for input type=\"date\")

In the following example MyDate starts o

8条回答
  •  遥遥无期
    2021-01-04 01:31

    The same as this custom binding, but using momentJS:

    ko.bindingHandlers.datePicker = {
        init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
            // Register change callbacks to update the model
            // if the control changes.
            ko.utils.registerEventHandler(element, "change", function () {
                var value = valueAccessor();
                value(moment(element.value).format());
            });
        },
        // Update the control whenever the view model changes
        update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
            var value =  valueAccessor();
            element.value = moment(value()).format("YYYY-MM-DD");
        }
    };
    

提交回复
热议问题