【发布时间】:2016-02-04 00:46:43
【问题描述】:
我可以通过这样做在 Knockout.js 中创建一个伪选择列表:
<div style="width:325px;height:500px;overflow:auto;" data-bind="template: { name: 'group-tmpl', foreach: explorer().categoryData }"></div>
但您不会像使用 <select> 那样获得样式和选定值。
我尝试执行此操作,但收到一条错误消息,提示您无法在此数据绑定中使用模板。
<select data-bind="options: explorer().categories, value: explorer().selectedCategory, optionsText:'name', template: 'group-tmpl'" size="15" />
我还尝试在<option> 中指定一个模板,但没有正确呈现(刚刚看到[object Object] 的列表)。
我想要的东西之一是<option> 中的图像。所以我想我可以尝试在加载后使用 css & 使用 jQuery 来设置 attr,但这违背了使用 Knockout 的目的。
我希望我错过了一些明显和/或容易的事情。
【问题讨论】:
-
You can't 在原生
<option>元素中有图像或其他 HTML。您将需要使用您的第一个代码示例来模拟选择行为,并最终将其封装在自定义绑定中。
标签: javascript jquery html css knockout.js