单选按钮取消选中

时间:2011-12-19 16:44:32

标签: javascript jquery html forms radio-button

编辑*以下答案解决了问题,如果有人遇到同样的问题。

我的单选按钮有小问题我有三条线,每条线有3个单选按钮,我希望用户能够检查前两行的任何单选按钮,但只能在每一行上选择一个选项:

例如

用户只能从“keystage1yr1”中选择一个选项,并且只能从“keystage1yr2”中选择一个选项。这一切都很好,因为它们具有相同的“名称”,因此通过defualt,用户只能为每一行选择一个。

我试过了:

Deselect Radio Button if another one is selected

How do I deselect a whole group of radio buttons when a user clicks on some other button?

但要知道运气,尽管他们可能会帮助其他人。

<input class="radio" type="radio" name="keystage1yr1" value="Paper" /> <span>&#163;25</span>
<input class="radio" type="radio" name="keystage1yr1" value="CD" /> <span>&#163;25 excl VAT</span>
<input class="radio" type="radio" name="keystage1yr1" value="Paper & CD" /> <span>&#163;40 excl VAT</span>

<input class="radio" type="radio" name="keystage1yr2" value="Paper" /> <span>&#163;25</span>
<input class="radio" type="radio" name="keystage1yr2" value="CD" /> <span>&#163;25 excl VAT</span>
<input class="radio" type="radio" name="keystage1yr2" value="Paper & CD" /> <span>&#163;40 excl VAT</span>

<input class="radio" type="radio" name="keystage1save" value="Paper" /> <span>&#163;47.50</span>
<input class="radio" type="radio" name="keystage1save" value="CD" /> <span>&#163;47.50 excl VAT</span>
<input class="radio" type="radio" name="keystage1save" value="Paper & CD" /> <span>&#163;75 excl VAT</span>

提前致谢。

1 个答案:

答案 0 :(得分:1)

这里有一些Javascript可以帮助你:

// Simply caching the dom queries for speed
$yearstages = $('input[name="keystage1yr2"],input[name="keystage1yr1"]');
$savestages = $('input[name="keystage1save"]');
// I only attach one handler to some parent element:
$('#radios').on('change', 'input', function() {
    if (this.name == "keystage1save") {
        $yearstages.filter('input:checked').prop('checked', false);
    } else {
        $savestages.filter('input:checked').prop('checked', false);
    }
});

这项工作的一个例子:http://jsfiddle.net/CUTnY/