我使用Knockout,并使用值预先填充选择列表。我希望将这些值复制到文本框中,但让用户能够覆盖文本框中的任何内容。当前,当文本框失去焦点时,它的值将恢复为下拉列表的当前选定值。
ViewModel:
function ViewModel() {
var self = this;
self.services = ['/api/trainers', '/api/trainingcategories']
self.selectedApi = ko.observable('/api/trainers');
}HTML:
<div class="form-group">
<label>API</label>
<select data-bind="options: services,
optionsCaption: 'Choose an API...',
value: selectedApi,
valueAllowUnset: true"></select>
</div>
<div class="form-group">
<input class="form-control" type="text" data-bind="value: selectedApi" />
<button type="submit" class="btn btn-default">Call API</button>
</div>我以为只要将selectedApi值设置为不在选项列表中的值,设置valueAllowUnset就会清除select元素,但这不是我看到的行为。
发布于 2015-10-16 10:51:04
valueAllowUnset选项是在3.1.0版本的Knockout中添加的。必须使用该版本或更高版本才能使选项生效。
https://stackoverflow.com/questions/32742414
复制相似问题