如果选择了另一个,请取消选择单选按钮 [英] Deselect Radio Button if another one is selected
问题描述
考虑以下标记:
<form>
<div class="project">
<ul class="choice">
<li><label for="firstchoice_1">First choice</label><input type="radio" name="firstchoice" value="1" id="firstchoice_1" /></li>
<li><label for="secondchoice_1">Second choice</label><input type="radio" name="secondchoice" value="1" id="secondchoice_1" /></li>
</ul>
</div>
<div class="project">
<ul class="choice">
<li><label for="firstchoice_2">First choice</label><input type="radio" name="firstchoice" value="2" id="firstchoice_2" /></li>
<li><label for="secondchoice_2">Second choice</label><input type="radio" name="secondchoice" value="2" id="secondchoice_2" /></li>
</ul>
</div>
</form>
我想做的就是,如果每一个.project
组中的单选按钮都被选中,则禁用它(因此只能选择每个组中的First choice
或Second choice
).我知道该怎么做是在特定情况下禁用一个特定的单选按钮,但我不知道如何对此进行概括,因为它们可能是一百个这样的.project
组.
What I want to do is to disable one if the radio button in every .project
group whenever the other one was selected (so only First choice
or Second choice
in every group can be selected). What I know how to do is to disable one specific radio button in one specific case but I don't know how to generalize this as they may are a hundred of such .project
groups.
编辑:请注意,只能选择整个First choice
(反之亦然).该名称已被使用相同的名称.在整个标记中使用相同的name
属性.只有两个不同的name
.
Edit: Please note that only of the whole First choice
(and vice versa) can be selected. The same name is already used for that one. The same name
attribute is used throughout the whole markup. There are only two different name
s.
推荐答案
简单地给他们相同的name
但不同的value
s;然后,这将自动发生,因为这是单选按钮的设计方式.
Simply give them the same name
but different value
s; this will then happen automatically as this is how radio buttons are designed.
这篇关于如果选择了另一个,请取消选择单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!