Knockoutjs : How to add title to options in select

允我心安 提交于 2019-12-23 12:22:02

问题


I use Knockoutjs options binding to populate my selects as this

<select data-bind="value: val, options: options, optionsText: 'text', optionsValue: 'ID'">
</select>

But for some option, the text is the same. Then I would like to add a title attribute to add information. Can I do that with knockout without modifying knockoutjs itself ?

May be using a function in optionsText, but I can't see How


回答1:


You can do it using the foreach binding, like this:

<select data-bind="foreach: options, value: selectedValue">
    <option data-bind="value: ID, text: text, attr: {title: title}" ></option>
</select>

Here is a working example.



来源:https://stackoverflow.com/questions/12733430/knockoutjs-how-to-add-title-to-options-in-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!