点击文字select相应的单选button

我正在使用PHP创build一个测验Web应用程序。 每个问题由一个单独的<label>并有4个可能的select,使用radio buttons允许用户select他/她的答案。 单个问题的当前HTML如下所示:

 <label for="349">What is my middle name?</label> <br> <input id="349" type="radio" value="1" name="349">Abe <br> <input id="349" type="radio" value="2" name="349">Andrew <br> <input id="349" type="radio" value="3" name="349">Andre <br> <input id="349" type="radio" value="4" name="349">Anderson <br> 

我希望用户可以select单击与单选button关联的文本。 现在,用户只能点击单选button – 我发现这是一个非常麻烦的任务。

我阅读无法select一个特定的单选buttonselect,通过点击select文本和build议点使标签的forid属性匹配。 我已经做到了这一点,但仍然无法正常工作。

我的问题是: 我希望能够点击一个<input type="radio">对象的文本,而不是只能够select单选button本身。 我知道我已经阅读过这个,但似乎无法find任何解决scheme,我的问题。 任何帮助或build议非常感谢!

在你的代码中,你在表单本身上有一个标签。 您要在每个单独的广播组上放置标签,如下所示。

 <form> <p>What is my middle name?</p> <br> <input id="349" type="radio" value="1" name="question1"> <label for="349">Abe</label> <br> <input id="350" type="radio" value="2" name="question1"> <label for="350">Andrew</label> <br> <input id="351" type="radio" value="3" name="question1"> <label for="351">Andre</label> <br> <input id="352" type="radio" value="4" name="question1"> <label for="352">Anderson</label> <br> </form> 

按照Nathan的回答,单选button和标签之间似乎有一个不可调整的空间。 以下是如何使他们无缝连接(见本文 ):

 <form> <p>What is my middle name?</p> <br> <label><input id="349" type="radio" value="1" name="question1">Abe</label> <br> <label><input id="350" type="radio" value="2" name="question1">Andrew</label> <br> <label><input id="351" type="radio" value="3" name="question1">Andre</label> <br> <label><input id="352" type="radio" value="4" name="question1">Anderson</label> <br> </form> 

标签标签应该在每个答案的周围,例如围绕着安倍,安德鲁等等,而且每个答案都必须是唯一的。

你可以这样做

  <label for="1">hi</label> <input type="radio" id="1" /> 

所以如果你点击文字或标签,select单选button。 但是,如果你这样做…

 <label for="1">//</label> 

而且你把这个添加到我之前编写的代码中,如果你点击第二个标签,那么它也将select收音机。

将input标签嵌套在标签标签内可确保标签显示在单选button旁边。 使用前面提到的方法,您可以将标签标签放置在html文件的任何位置,并在单击时select相关的单选button。 看一下这个:

 <html> <body> <form> <p>What is my middle name?</p> <br> <input id="349" type="radio" value="1" name="question1"> <br> <input id="350" type="radio" value="2" name="question1"> <label for="350">Andrew</label> <br> <input id="351" type="radio" value="3" name="question1"> <br> <input id="352" type="radio" value="4" name="question1"> <label for="352">Anderson</label> <br> </form><br/> <p>This is a paragraph</p> <label for="349">Abe</label><br/> <label for="351">Andre</label> </body> </html>