【发布时间】:2013-02-05 11:45:14
【问题描述】:
我要获取选中的选项对象
<select data-bind="options: availableCountries,
value: selectedCountry, event: { select: onSelect}"></select>
<script type="text/javascript">
// Constructor for an object with two properties
var Country = function(name, population) {
this.countryName = name;
this.countryPopulation = population;
};
var viewModel = {
availableCountries : ko.observableArray([
new Country("UK", 65000000),
new Country("USA", 320000000),
new Country("Sweden", 29000000)
]),
selectedCountry : ko.observable(), // Nothing selected by default
onSelect: function(){
console.log(viewModel.selectedCountry)
// it is showing just an country name and what i what is whole object
// e.g. { "UK", 65000000 } // that is selected option in selected box
}
};
</script>
【问题讨论】:
-
您可以在 JSFiddle 中发布您的真实代码吗?因为你所展示的应该可以看到:jsfiddle.net/Q7A76
-
只需删除
optionsValue:'name'即可正常工作:jsfiddle.net/9MKce/4 -
感谢@nemesv,但它会在数据绑定时产生问题