我正在尝试将此 jquery颜色选择器与 knockout.js 一起使用。我已经编写了自定义绑定处理程序来将颜色选择器输入控件与我的 viewModel 颜色值绑定。
这是绑定处理程序代码:
ko.bindingHandlers.colorPicker = {
init: function (element, valueAccessor, allBindingsAccessor) {
//initialize datepicker with some optional options
var options = allBindingsAccessor().colorPickerOptions || {};
$(element).colorPicker(options);
//handle the field changing
ko.utils.registerEventHandler(element, "change", function () {
var observable = valueAccessor();
observable($(element).colorPicker("value"));
});
//handle disposal (if KO removes by the template binding)
ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
$(element).colorPicker("destroy");
});
},
update: function (element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
$(element).colorPicker("value", value);
}
和 HTML:
<input type="text" data-bind="colorPicker: newEvent().color, colorPickerOptions: { value:newEvent().color }"/>
问题是,当我更改颜色时,每次更改颜色时都会创建多个 div,如图所示。
谁能确定我的代码中有什么问题?