单击文本时选中或取消选中单选按钮

时间:2016-11-10 09:11:13

标签: html radio-button

<input type="radio" name="allotdeallot" value="Allotment" />Allotment
<input type="radio" name="allotdeallot" value="De-Allotment" />De-Allotment

我有两个单选按钮,我想在标签点击时检查或取消选中。

2 个答案:

答案 0 :(得分:1)

使用<label>代码并将id属性添加到radio按钮:

<input type="radio" name="allotdeallot" id="allotment" value="Allotment" /><label for="allotment">Allotment</label>
<input type="radio" name="allotdeallot" id="deallotment" value="De-Allotment" /><label for="deallotment">De-Allotment</label>

More information

答案 1 :(得分:0)

根据HTML标准,每个表单元素都必须附加label。您希望隐藏标签文字,可以使用lable隐藏position:absolute的文字label。所以这是你的答案,

<input id="allotment" type="radio" name="allotdeallot" value="Allotment" /><label for="allotment">Allotment</label>
<input id="de-allotment" type="radio" name="allotdeallot" value="De-Allotment" /><label for="de-allotment">De-Allotment</label>

id of the input field必须与for attribute of the label相同。