我有一个使用在DB的答案作为名称的IDS,以检查他们是否是正确的测验单选按钮。所以基本上我不能让它们成为同一个名字。使用jQuery选择/取消选择使用不同的名称
不管怎么说这给我留下了,可所有的检查,这是不是它应该如何工作的单选按钮。我发现jQuery允许单选按钮被取消选中,但我需要它们在您点击其他按钮时取消选中。
HTML:
<fieldset>
<input type="radio" name="21">
<label for="21">Answer 21</label>
<input type="radio" name="22" class="checked">
<label for="22">Answer 22</label>
<input type="radio" name="23" class="">
<label for="23">Answer 23</label>
<input type="radio" name="24">
<label for="24">Answer 24</label>
</fieldset>
脚本:
$("input[type='radio']").click(function (event) {
// If the button is selected.
if ($(this).hasClass("checked")) {
// Remove the placeholder.
$(this).removeClass("checked");
// And remove the selection.
$(this).removeAttr("checked");
// If the button is not selected.
} else {
// Remove the placeholder from the other buttons.
$("input[type='radio']").each(function() {
$(this).removeClass("checked");
});
// And add the placeholder to the button.
$(this).addClass("checked");
}
});
所以,如果21被选中,我点击22,21应取消
这也适用于Fieldset。对于正确答案的初始选择不适用于多个字段集。谢谢! – FranticJ3