禁用所有具有相同名称的单选按钮

时间:2015-06-13 18:40:15

标签: jquery

我有一组2个单选按钮,如果选择了其中一个选项,则必须禁用同一组中的其他无线电。

<div class="question_container">
<p class="questions">Question about stuff?</p>  <span class="group" style="inline-block;">
    <label>
    <input id="id_radio1" class="incorrect" type="radio" name="group1" value="Answer 1" />Answer 1
    </label>
    <label>
    <input id="id_radio2" class="correct" type="radio" name="group1" value="Answer 2" />Answer 2
    </label>
    <label>
    <input id="id_radio3" class="incorrect" type="radio" name="group1" value="Answer 3" />Answer 3
    </label>
    <label>
    <input id="id_radio4" class="incorrect" type="radio" name="group1" value="Answer 4" />Answer 4
    </label>
</span>

    关于东西的问题?

                 答案1                           答案2                           答案3                           答案4              
$("input[type=radio]").click(function () {
var Radio = $(this);
var GetGroup = Radio.parents(".group");
GetGroup.find("label").css({
    "background-color": "transparent"
});

if (Radio.attr("class") == 'correct') {

    Radio.parent().css({
        "background-color": "green"
    });
} else Radio.parent().css({
    "background-color": "red"
});

});

Demo

3 个答案:

答案 0 :(得分:0)

只需将其添加到点击处理程序的末尾:

$('input[name=' + Radio.context.name + ']').attr("disabled",true);

它会禁用相关群组......

答案 1 :(得分:0)

var RadioName = Radio.attr('name');
$("input[name='" + RadioName + "']").attr("disabled","disabled");

答案 2 :(得分:0)

要禁用具有相同name属性的所有广播,请使用prop功能:

//disable all radios with the name 'insertyourradioname'
$('input[name=insertyourradioname]').prop('disabled', true);

//enable all radios with the name 'insertyourradioname'
$('input[name=insertyourradioname]').prop('disabled', false);