【发布时间】:2018-02-01 10:21:35
【问题描述】:
我有一个 observableArray,它由 ajax 调用填充,看起来像
appModel.foo().bar().fooBarCategories()
[
Object:
id: Object
type: "integer"
value: "4986517"
name: "Adwords"
,
Object
,
Object,
...
]
我有一个选择,我希望 optionsText 成为“名称”属性和值“id.value”属性
喜欢:
<select id="fooBarCategory"
data-bind="
options: appModel.foo().bar().fooBarCategories(),
optionsText: name,
value: id.value"
></select>
但它不起作用说:
Uncaught ReferenceError: Unable to process binding "value: function (){return id.value }" 消息:id 未定义
那么我该如何实现呢?尝试过value:$data.id.value或
value:this.id.value
这就是我如何使用 name 属性:
optionsText:function(item) {
return item.name
}
但是如果和id属性类似
value:function(item){ return item.id.value }
然后生成的选项中的值 attr 保持空白
你有一个小提琴http://jsfiddle.net/q65nz/1/
【问题讨论】:
-
查看更新后的答案。注意
optionsValue而不是value
标签: javascript object knockout.js properties