0

假设我有一个选择框,例如

<div data-bind='visible: someProp'>   
   <select class="selectSubWidgets" data-bind='options: subWidgets,optionsText:          "Name", optionsValue : "Name", optionsCaption: "[Select a Widget]",attr: {name: "Widgets[" + $parent.widgets.indexOf($data) + "].Name"}, value: selectedSubWidget'></select>
</div>

如果此选择中的项目数仅为 1 - 目前我隐藏此下拉列表(工作)但我还想确保选择单个项目(隐藏时)(因此第一个值绑定在表单帖子中)。有没有办法表达这种绑定?

谢谢!

4

1 回答 1

1

那么所选元素受以下约束:

value: selectedSubWidget

所以你应该简单地用你的默认值初始化 selectedSubwidget 。

this.selectedSubWidget = ko.observable("defaultValue");
于 2012-07-03T07:36:50.497 回答