将默认选择选项设置为列表中的第一个项目

Mat*_*ttl 0 knockout.js

在淘汰示例页面上使用淘汰赛车演示,是否有人知道如何更改它以便"产品"选择始终默认为选择框中的第一个产品?

Jef*_*ado 6

使用options绑定时,也可以识别其他绑定.在提供的示例中,optionsText是一个绑定,它确定每个对象的哪个属性将显示为文本.示例中使用的另一个属性是optionsCaption.此绑定设置在未选择任何内容时为选择框显示的初始文本.

如果要更改它,以便选择框以列表中的第一个值开头,只需删除optionsCaption绑定即可.假设value未设置绑定到绑定的可观察对象,它将默认为数组中的第一个项目.

改变这个:

<select data-bind="options: sampleProductCategories,
                   optionsText: 'name',
                   optionsCaption: 'Select...',
                   value: category"></select>
Run Code Online (Sandbox Code Playgroud)

对此:

<select data-bind="options: sampleProductCategories,
                   optionsText: 'name',
                   value: category"></select>
Run Code Online (Sandbox Code Playgroud)