简体   繁体   English

在表格单元格内分布单选按钮

[英]Spread radio button inside table cell

I need to have table with two columns. 我需要有两列的表。 Each row is one radio button pair. 每行是一对单选按钮。

For example: There is question and two possible answers yes/no. 例如:有一个问题,两个可能的答案是/否。

+------+------+
| yes  | no   | - 1.question
| yes  | no   | - 2.question
| yes  | no   | - 3.question
| yes  | no   | - 4.question
| yes  | no   | - 5.question
+------+------+

The problem is I need to spread radio button to whole cell. 问题是我需要将单选按钮扩展到整个单元格。 If someone wants to select choice it is not comfortable to target only on text... How to do that 如果有人要选择选项,那么仅以文本为目标是不舒服的...如何做到这一点

<table>
  <tr>
    <td>
      <input type="radio" name="first">yes</radio>
    </td>
    <td>
      <input type="radio" name="first">no</radio>
    </td>
    <td>
      1.Question
    </td>
  </tr>
  <tr>
    <td>
      <input type="radio" name="second">yes</radio>
    </td>
    <td>
      <input type="radio" name="second">no</radio>
    </td>
    <td>
      2.Question
    </td>
  </tr>...
</table>

One issue with your HTML: you did not specify the name attribute for the input elements. HTML的一个问题:您没有为输入元素指定name属性。 Remember that if you're doing this, no data will be sent because there is simply no fragment identifier for the form data. 请记住,如果执行此操作, 则不会发送任何数据,因为表单数据根本没有片段标识符。

OP seems to have fixed the issue for the missing name attribute. OP似乎已解决了缺少name属性的问题。


Use <label for="..."> to target each radio button by id within the table cell, and set it to a block level element such that it fills up the entire space in the cell. 使用<label for="...">在表单元格中按id定位每个单选按钮,并将其设置为块级元素,使其填充单元格中的整个空间。 Example markup: 标记示例:

 label { cursor: pointer; display: block; text-align: center; } 
 <table> <tr> <td><label for="q1-y"><input type="radio" name="q1" id="q1-y" value="yes" />Yes</label></td> <td><label for="q1-n"><input type="radio" name="q1" id="q1-n" value="no" />No</label></td> <td>Question 1</td> </tr> </table> 

See proof-of-concept demo here: http://jsfiddle.net/teddyrised/zrjy29bw/ 在此处查看概念验证演示: http : //jsfiddle.net/teddyrised/zrjy29bw/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM