Knockout.js 如何更新选定文本以及敲除绑定下拉列表中的值
我的下拉列表HTML:Knockout.js 如何更新选定文本以及敲除绑定下拉列表中的值,knockout.js,Knockout.js,我的下拉列表HTML: <div class="col-sm-10"> <select id="gender" class="form-control" data-bind=" value: gender.id(), options: $root.genders, optionsText: 'name', optionsCaption:'Select
<div class="col-sm-10">
<select id="gender" class="form-control"
data-bind="
value: gender.id(),
options: $root.genders,
optionsText: 'name',
optionsCaption:'Select ...',
optionsValue:'id' ">
</select>
</div>
问题是,当我更改下拉列表中的选项时,孩子的性别id只会更新,而不会更新name属性
我还尝试将text
绑定到gender.name()
,但没有成功
可观察到的性别:
"genders": [
{
"id": 1,
"name": "Male"
},
{
"id": 2,
"name": "Female"
},
{
"id": 3,
"name": "Other"
}
],
在选项值中使用$data而不是id。它绑定数据dogender\u id,您的选择框应如下所示:
<select id="gender" class="form-control"
data-bind="value: gender_id,
options: $root.genders,
optionsText: 'name',
optionsCaption:'Select ...',
optionsValue:$data ">
</select
制作一个提琴样品,这样我们就可以到处玩,很容易给你一个答案。为这样做的困难喝彩试着看@haim770:你能详细说明一下逻辑吗?:)如果您有一个性别
的查找列表,则具有关联性别
的任何实体(例如子实体)都需要使用引用对该关联进行建模,而不是复制每个实体的性别
值对象。为每个实体提供完整的gender
对象仍然很方便,但是,在我的示例中,它使用一个计算函数来完成,该函数使用gender\u id
检索对相应gender
的引用。
<select id="gender" class="form-control"
data-bind="value: gender_id,
options: $root.genders,
optionsText: 'name',
optionsCaption:'Select ...',
optionsValue:$data ">
</select