admin管理员组文章数量:1394143
I'm creating a form from various plex controls that I have built as Backbone views. Understandably I want to reliably link the labels to the <input>
elements, which I am doing with the normal for
attribute.
However, sometimes I need to use the same control multiple times. I am using data attributes to drive the form, so I do not need the id
attribute for my own use, and can use classes to identify each control.
Therefore, I am considering whether it makes sense to generate random ids, just to link the <label>
and <input>
together? This seems a really bad idea, but I am unsure there is a better one?
I can't just put the <input>
inside the <label>
, as they have to be separate from each other.
I'm creating a form from various plex controls that I have built as Backbone views. Understandably I want to reliably link the labels to the <input>
elements, which I am doing with the normal for
attribute.
However, sometimes I need to use the same control multiple times. I am using data attributes to drive the form, so I do not need the id
attribute for my own use, and can use classes to identify each control.
Therefore, I am considering whether it makes sense to generate random ids, just to link the <label>
and <input>
together? This seems a really bad idea, but I am unsure there is a better one?
I can't just put the <input>
inside the <label>
, as they have to be separate from each other.
2 Answers
Reset to default 2There's nothing bad in auto-generating IDs. If they have not to be human- (== developer) readable, you can go crazy there. Create a simple function, that spits out unique strings, and there you go:
function generateId() {
return 'GENERATED_ID_' + (++generateId.counter);
}
generateId.counter = 0;
id = generateId();
html = '<label for="'+id+'">Foo</label> <input id="'+id+'">';
Nothing bad happening here.
(Of course, if you could nest the input in the label, that would be a wee bit nicer.)
I would use the cid
attribute of the Backbone view. This is already unique. Probably then override _.template
in a base view to always include this (to save passing in each time).
本文标签: javascriptUsing auto generated (ie random) IDs in HTML formsStack Overflow
版权声明:本文标题:javascript - Using auto generated (ie random) IDs in HTML forms - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744663842a2618411.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论