我正在学习 Knockout,并且在处理下拉绑定(bind)时遇到了问题。基本上,它是一个可以附加 1 个或多个诊断代码的患者模型。
如果您查看我的 jsfiddle,我有一个带有名称/值对 (axis1items) 的对象,用于保存选择列表项。我还有一个 observablearray 保存所有结果 (patentDiags),然后将其序列化为 JSON。
当您选择一个项目时,该值将设置为 axis1items 项目,并序列化为:
"DiagnosisID":{"dxname":"(V71.81) Abuse and neglect","dxvalue":549}
我希望最终的序列化结果只是 dxvalue:
"DiagnosisID": 549
我认为这可以使用计算值轻松完成,更改我的 View 模型的结构,或 some other knockout specific template keyword that I am overlooking ?
此外:任何有关如何改进我的代码的其他建议将不胜感激!
最佳答案
你就快到了,你只需要设置 optionsValue
给您'dxvalue'
属性(property),其中 optionsValue
parameter
Similar to
optionsText
, you can also pass an additional parameter calledoptionsValue
to specify which of the objects’ properties should be used to set thevalue
attribute on the<option>
elements that KO generates.
所以你的options
绑定(bind)应该如下所示:
<select data-bind="options: $root.axis1items,
optionsText: 'dxname',
optionsValue: 'dxvalue',
value: DiagnosisID" class="form-control required">
</select>
演示 JSFiddle .
关于javascript - knockout 下拉选择列表将名称/值对数组绑定(bind)到 observablearray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24113442/