在 Knockout Foreach 循环中生成 ID
我正在尝试使用 Knockout 构建一些 HTML,Jquery UI 可以将其转换为 切换按钮。我需要达到的是:
<div id="status">
<input type="radio" id="status_ACTIVE" value="ACTIVE" name="status" /><label for="status_ACTIVE">Active</label>
<input type="radio" id="status_INACTIVE" value="INACTIVE" name="status" checked="checked" /><label for="status_INACTIVE">Inactive</label>
</div>
使用 JQuery UI 我可以轻松地将其变成切换按钮。但是,如何在不使用现在已弃用的 JQuery 模板的情况下生成它呢?这就是我试图做的:
在 javascript 模型内部:
self.statuses = [{Selected:true,Text:"Active",Value:"ACTIVE"},{Selected:false,Text:"Inactive",Value:"INACTIVE"}];
标记:
<div id="status" data-bind="foreach: statuses">
<input type="radio" name="status" data-bind="value: Value, id: 'status_' + Value" /><label data-bind="text: Text, for: 'status_' + Value"></label>
</div>
这不起作用。我认为它不喜欢我尝试创建该 ID 或将其与循环中的 for 关联的方式。它错误地绘制了按钮,因为两个独立的按钮并且单击功能不起作用。
即使我只是将值指定为 id,例如:id: Value
和 for: Value
它仍然不起作用。我可以不使用淘汰赛来设置这些属性吗?
I'm trying to build some HTML with Knockout that Jquery UI can turn into toggle buttons. What I need to arrive at is this:
<div id="status">
<input type="radio" id="status_ACTIVE" value="ACTIVE" name="status" /><label for="status_ACTIVE">Active</label>
<input type="radio" id="status_INACTIVE" value="INACTIVE" name="status" checked="checked" /><label for="status_INACTIVE">Inactive</label>
</div>
Using JQuery UI I can easily turn that into toggle buttons. But how do I generate that without using the now depreciated JQuery Templates? This is what I attempted to do:
Inside the javascript model:
self.statuses = [{Selected:true,Text:"Active",Value:"ACTIVE"},{Selected:false,Text:"Inactive",Value:"INACTIVE"}];
The markup:
<div id="status" data-bind="foreach: statuses">
<input type="radio" name="status" data-bind="value: Value, id: 'status_' + Value" /><label data-bind="text: Text, for: 'status_' + Value"></label>
</div>
This doesn't work. I don't think it likes how I'm trying to create that ID, or associate it with the for in the loop. It draws the buttons incorrectly, as two independent buttons and the click functionality doesn't work.
Even if I just specify the value as the id like: id: Value
and for: Value
it still doesn't work. Can I not set these attributes using knockout?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加此 javascript 解决了我的问题:
我确实必须将
for: 'status_' + Value
更改为foratt: 'status_' + Value
asfor
是一个保留字。更新:
我也可以使用“attr”绑定,如下所示:
Adding this javascript solved my issue:
I did have to change
for: 'status_' + Value
intoforatt: 'status_' + Value
asfor
is a reserved word.Update:
I could have also used the "attr" binding, like this: