Html 标签不适用于复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6712029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
label not working with checkbox
提问by Phillip Senn
OK, what am I missing? I have:
好吧,我错过了什么?我有:
<form>
<input type="checkbox" name="showRatings" value="1" checked>
<label for="showRatings">Show Ratings</label>
</form>
And when I click on the "Show Ratings" text, the checkbox is not toggling. I know it's something simple.
当我点击“显示评级”文本时,复选框不会切换。我知道这很简单。
回答by David
回答by prashantsahni
When input element is inside the label then we do not need id on the element and 'for' attribute on the label, but when it is outside we need it.
当输入元素在标签内时,我们不需要元素上的 id 和标签上的 'for' 属性,但当它在标签外时,我们需要它。
<label>
Foo
<input name="foo" type="checkbox" />
</label>
Clicking on "Foo" will trigger a toggle of the checkbox
单击“Foo”将触发复选框的切换