Html 带有可点击标签的无线电组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4684382/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 06:08:47  来源:igfitidea点击:

Radio Groups with clickable label

htmlformswebforms

提问by Racky

From what I have gathered, in order to make a label of a radio button clickable, you must assign the same "name" attribute value to both elements.

根据我收集的信息,为了使单选按钮的标签可点击,您必须为两个元素分配相同的“名称”属性值。

The problem I am having is when you have more than one radio button, say a "yes or no" type selection. In order to make it to where if you click one, the other disables is that both radio buttons "name" attribute has to be the same value.

我遇到的问题是当您有多个单选按钮时,请说“是或否”类型选择。为了使它在单击一个时到达哪个位置,另一个禁用是两个单选按钮的“名称”属性必须是相同的值。

Is it possible to do both?

两者都可以吗?

<label for="no">No</label> 
<input type="radio" name="no" value="no" />

<label for="yes">Yes</label> 
<input type="radio" name="yes" value="yes" />

回答by Ish

id(not name) attribute should be referred by label's forattribute. It should be like this: http://jsfiddle.net/zzsSw/

id(不是 name) 属性应该由标签的for属性引用。应该是这样的:http: //jsfiddle.net/zzsSw/

<label for="no">No</label> 
<input type="radio" name="mygroup" id="no" value="no" />

<label for="yes">Yes</label> 
<input type="radio" name="mygroup" id="yes" value="yes" />

回答by Racky

You can also write labels without IDs:

您还可以编写没有 ID 的标签:

<label>
  <input type="radio" name="mygroup" />
  My clickable caption
</label>

or checkbox

或复选框

<label>
  <input type="checkbox" name="mygroup[]" />
  My clickable caption
</label>