Html 您如何使单选按钮文本也可点击?

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

How do you make the radio button text to be clickable too?

htmlradio-button

提问by Faito

i have this radio button (and some other after this one):

我有这个单选按钮(还有其他一些):

<input type="radio" name="group1" value="Milk"> Milk<br>

But if i want to activate the radio button, i have to click it, clicking the word "Milk" which is to the right of the button, doesn't activate it. How can i do that?, all the examples i found about radio buttons so far, have the same issue. Thanks in advance.

但是如果我想激活单选按钮,我必须点击它,点击按钮右侧的“牛奶”这个词,不会激活它。我该怎么做?到目前为止,我发现的所有关于单选按钮的例子都有同样的问题。提前致谢。

回答by Nathan Lafferty

Here you want to use the label tag.

这里要使用label标签。

Something like:

就像是:

            <label>
                <input type="radio" name="group1" value="Milk">
                Milk
            </label><br/>

Labels tell the browser that everything contained within should be treated as one element (in terms of text. They are not divs)

标签告诉浏览器中包含的所有内容都应视为一个元素(就文本而言。它们不是 div)

Take a look at this for an example: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_label

看看这个例子:http: //www.w3schools.com/tags/tryit.asp?filename=tryhtml_label

回答by Mansfield

If you use the label tag, giving your radio button an ID, you should be able to click on the label to select the radio.

如果您使用 label 标签,为您的单选按钮提供一个 ID,您应该能够点击标签来选择单选按钮。

<input type="radio" name="group1" value="Milk" id="rad1">
<label for="rad1">Milk</label>

http://jsfiddle.net/tvFgU/1/

http://jsfiddle.net/tvFgU/1/

This is valid for xhtml 1.0 strict:

这对 xhtml 1.0 严格有效:

enter image description here

在此处输入图片说明

回答by Anupam Maurya

For React JSX

对于 React JSX

<input type="radio" name="group" value="happy" id="rad1">
<label htmlFor="rad1">YES</label>

<input type="radio" name="group" value="sad" id="rad2">
<label htmlFor="rad2">NO</label>

Here Name attribute would be the same for both radio button.

这里的 Name 属性对于两个单选按钮都是相同的。