可以将标签与checkbox相关联,而不使用“for = id”? [英] Possible to associate label with checkbox without using "for=id"?
问题描述
我知道有时可以将标签与复选框相关联:
I know that it is good sometimes to associate a label with a checkbox:
<input id="something" type="checkbox" value="somevalue" />
<label for="something">this is label text</label>
..但我是否已经使用ID来关联它?
我甚至关心的主要原因是因为我喜欢单击一个标签来切换复选框的值,但不喜欢使用id这么简单的想法。
..but do I have to use an id to associate it?
The main reason I even care is because I like being able to click a label to toggle the checkbox value, but don't like the idea of using an id for something so simple.
我想我可以使用jQuery切换点击的标签的前一个元素(复选框),但也许有一些更简单我错过。 http://stackoverflow.com/a/2720771/923817 看起来像一个解决方案,但用户说它不工作在IE。
I guess I could use jQuery toggle the previous element (checkbox) of a clicked label, but maybe there is something simpler I'm missing. http://stackoverflow.com/a/2720771/923817 looked like a solution, but the user said it doesn't work in IE.
推荐答案
是的,将输入放在标签内。
Yes, place the input inside the label.
<label><input type=checkbox name=chkbx1> Label here</label>
请参阅 隐含标签关联 。
See implicit label association in the HTML specifications.
这篇关于可以将标签与checkbox相关联,而不使用“for = id”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!