当我以编程方式更新可观察对象并打开模态时,为什么下拉菜单没有更新?
如果我尝试先更改元素的值,则更改可观察项会更新下拉列表。单击“启动”模态按钮,然后单击“可观察项”按钮。选定的ID会更改,但下拉列表不会更改。现在单击“元素”按钮,然后再次单击“可观察项”按钮。下拉列表会更改。
Javascript语言
this.selectedSimulators = ko.observableArray().extend({notify: 'always'});
this.simulators = ko.observableArray([
new Simulator(1, 1, 1, "CH-53E", "APT", "APT 2F190-2"),
new Simulator(2, 1, 1, "CH-53E", "EAET", "EAET 2H164-2"),
new Simulator(3, 1, 1, "CH-53E", "WST", "WST 2F174-2")
]);
this.openModal = function() {
$('#exampleModal').modal('show');
this.selectedSimulators(1);
// UI does not update without calling this
//$("#ddSims").val(1);
}
HTML格式
<select class="form-control" id="ddSims" multiple="multiple" data-bind="options: simulators,
optionsText: 'typeAndSerialNumber',
optionsValue: 'id',
selectedOptions: selectedSimulators,
multiselect: {includeSelectAllOption: true}">
</select>
<button type="button" class="btn btn-primary" data-bind="event: {click: openModal}">
Launch modal
</button>
1条答案
按热度按时间lb3vh1jj1#
您混合了observables和observableArrays。selectedSimulators是一个数组,但您将其设置为值1,而不是将值1推入数组。请尝试
self.selectedSimulators.push(1);
。