我有一个多选择的下拉列表,我将一个对象绑定到那个.Just以从选择中获取多个值,我能够得到值并处理它。
下面是我如何在页面中加载多选择控件的示例
http://jsfiddle.net/Hsakarp/d5j0zogz/
var viewModel = {
optionValues: [{name: "name1", Id: 1, fullname: "Development"},{name: "name2", Id: 2, fullname: "Development"},{name: "name3", Id: 3, fullname: "Development"}],
multipleSelectedOptionValues: ko.observable(),
};但是,当我再次加载该页面进行编辑时,我希望将这些值设置为选定的值,我已尝试使用jQuery-JQuery multiselect - Set a value as selected in the multiselect dropdown。
有人能告诉我怎么用Knockout自己做吗?
发布于 2015-03-05 08:34:22
您需要设置“optionsValue”并将其填充到可观察的数组中。
html:
<select multiple="multiple" data-bind="options: optionValues, optionsValue: 'Id', optionsText:'name', selectedOptions: multipleSelectedOptionValues"></select>击倒:
var viewModel = {
optionValues: [{name: "name1", Id: 1, fullname: "Development"},{name: "name2", Id: 2, fullname: "Development"},{name: "name3", Id: 3, fullname: "Development"}],
multipleSelectedOptionValues: ko.observableArray([1,3]),
};
ko.applyBindings(viewModel);我为您创建了一个更新的小提琴:http://jsfiddle.net/d5j0zogz/2/
正如Tomalak所指出的,淘汰赛的文档非常好,但需要一些时间才能适应。通过它来帮助你理解答案是个好主意。
https://stackoverflow.com/questions/28872495
复制相似问题