将 View 模型绑定(bind)到选择元素后,然后从该元素获取值。它返回数组内选定的值。
例如:所选值为“1”, View 模型变量的值为“[1]”。
<label>Customer:</label>
<select class="form-control" data-bind="options: [1, 2],
selectedOptions: Customer"></select>
<button data-bind="click: $root.Click">Test</button>
function AppViewModel() {
var self = this;
this.Customer = ko.observable();
this.Click = function(){console.log(self.Customer());}
}
// Activates knockout.js
var temp = new AppViewModel();
ko.applyBindings(temp);
我有其他选择的元素可以执行此操作,而其他元素则没有,并且我一生都无法看到其中的差异。任何帮助将不胜感激。
最佳答案
<label>Customer:</label>
<select class="form-control" data-bind="options: [1, 2], value: Customer"></select>
<button data-bind="click: $root.Click">Test</button>
我不太确定你的问题是什么,但据我可以推断。
我一直使用 value
进行单选,并在多选时使用 selectedOptions
关于javascript - knockout : Select Binding Returns Array of Values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24441054/