admin管理员组文章数量:1277875
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?
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?
- Looks like there isn't a binding for the attributes "id" or "for." I need to write a custom binding for it. knockoutjs./documentation/custom-bindings.html – Arbiter Commented Jan 25, 2012 at 20:03
1 Answer
Reset to default 10Adding this javascript solved my issue:
ko.bindingHandlers.id = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
$(element).attr('id', valueAccessor());
}
};
ko.bindingHandlers.forattr = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
$(element).attr('for', valueAccessor());
}
};
I did have to change for: 'status_' + Value
into foratt: 'status_' + Value
as for
is a reserved word.
Update: I could have also used the "attr" binding, like this:
attr: { for: 'status_' + Value }
本文标签: javascriptGenerating IDs within Knockout Foreach loopsStack Overflow
版权声明:本文标题:javascript - Generating IDs within Knockout Foreach loops - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741245164a2364734.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论