可以在不使用“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 切换点击标签的前一个元素(复选框),但也许我缺少一些更简单的东西.https://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. https://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>
参见隐式标签关联 在 HTML 规范中.
See implicit label association in the HTML specifications.
这篇关于可以在不使用“for=id"的情况下将标签与复选框相关联吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!