我有一個綁定到observable數組的元素,但是當observable數組中的值發生更改時,下拉列表不會更改。我究竟做錯了什麼?我希望能夠選擇一個可觀察對象,更改其中的一個值並使其下拉反映這些更改。在jsfiddle中,只需在下拉列表中選擇一個值,更改文本,然後單擊更新。當ObservableArray改變時Knockout Dropdown值不會改變
的Javascript:
var ViewModel = function() {
self.programs = ko.observableArray([
{programId: 1, programDescription: 'One'},
{programId: 2, programDescription: 'Two'},
{programId: 3, programDescription: 'Three'}
]);
self.program = ko.observable();
self.saveProgram = function() {
for (i = 0; i < self.programs().length - 1 ; i++) {
if (self.programs()[i].programId == self.program().programId) {
self.programs()[i].programDescription =
self.program().programDescription;
alert(self.programs()[i].programDescription);
}
}
};
};
ko.applyBindings(new ViewModel());
HTML:
<div>
<select data-bind="options: programs,
optionsText: 'programDescription',
value: program"></select>
</div>
<div>
Update Program Description: <input type="text" data-bind="value: program().programDescription" />
<button type="button" data-bind="click: saveProgram">Update</button>
</div>
預先感謝您