Knockout.js 如何更新选定文本以及敲除绑定下拉列表中的值

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

我的下拉列表HTML:

<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