如何通过单击选择选项仅触发单选按钮

时间:2013-07-30 02:43:01

标签: jquery onclick radio-button

我希望有一个单选按钮,只能通过选择选项的“onclick”触发。我不希望用户能够手动更改单选按钮。有没有办法实现这一目标。这是我用于单选按钮更改的代码(它可以工作),但用户可能仍然会在事后更改它:

$("#button1").prop('checked',true);
$("#strategies").mouseup(function() {
    if(strat == $("#strategies").val()) {
        $("#button1").prop('checked',true);
        $("#why").hide();
        $("#whyNo").show();
    }
    else {
        $("#button2").prop('checked',true);
        $("#whyNo").hide();
        $("#why").show();
    }

});

HTML:

<input type="radio" id="button1" name="switch" value="0">No Switch<br>
<input type="radio" id="button2" name="switch" value="1">Switch<br>

任何建议都将不胜感激。

2 个答案:

答案 0 :(得分:3)

在HTML中,输入字段的属性称为禁用。但是,我不确定如何在Javascript中操作它。

<input type="radio" disabled>

<input type="radio" disabled="disabled">

请注意,某些浏览器可能会将其渲染为“灰色”。

w3schools input tags

w3c recommendations

答案 1 :(得分:0)

你试过这个吗?

$('input[name="switch"]').click(function () {
    return false;
});