在这个视图模型中,我将一个下拉列表与对象绑定。当我想从下拉列表中清除项目时,我为所选项目提供了“null”,但它确实清除了下拉列表但不会删除所选项目。请看小提琴。http://jsfiddle.net/aroor/Su8Zq/36/
<select data-bind="optionsCaption: ' ', options: stations, optionsText : 'name' ,value: selectedStation"> </select>
<button data-bind="click: clearSelectedStation">Clear</button>
<br>
<span data-bind='text : selectedStation().name'></span>
var ClearSelectionViewModel = function () {
var self = this;
self.station= ko.observable();
self.selectedStation = ko.observable();
self.stations = ko.observableArray([{name :'CLT'},{ name : 'PHL'},{ name :'PHX'},{ name :'PIT'}]);
self.clearSelectedStation = function () {
self.selectedStation(null);
};
};
ko.applyBindings(new ClearSelectionViewModel());