Knockout does not sync manual set of option's selected attribute

℡╲_俬逩灬. 提交于 2020-01-14 17:35:54

问题


I'm using this jQuery plugin for a multi select list box http://www.quasipartikel.at/multiselect/, and it's all bound to a view model using knockoutjs.

The plugin sets the option's selected attribute when an item is selected or deselected. But knockout is obviously not checking for change in that attribute and so my view model is not being updated.

Now before I change the plug in and write a custom binding is there a way to tell knockoutjs to monitor the selected attribute?


回答1:


You can probably just set knockout to handle click events instead, that would be the easiest. For example

To Select, add on right side <li> or whatever you think is best

data-bind="click: function(){ select(country); }"

To Deselect, add on left side <li> or whatever you think is best

data-bind="click: function(){ deselect(country); }"

And then you also need handlers to add/remove/move between observableArrays

viewModel = {
  select: function(){ /* add to observableArray */ },
  deselect: function(){ /* remove from observableArray */ }
};


来源:https://stackoverflow.com/questions/7065290/knockout-does-not-sync-manual-set-of-options-selected-attribute

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