如何在jquery中取消选中已检查的单选按钮?

时间:2013-12-25 01:01:44

标签: jquery radio-button radiobuttonlist

我想要做的很简单。看看这个表格:

<input type="radio" id="Car"><label for="Car">Car</label>
<input type="radio" id="Bike"><label for="Bike">Car</label>

使用以下脚本:

$("input:radio").button().click(function(event){
    $(this).siblings().prop('checked', false);
});

现在这段代码不会使单选按钮互斥。如果我将“radio”改为“checkbox”,一切正常。但是我想知道为什么这对无线电输入不起作用?这是jQuery的错误还是我遗漏了一些重要的东西?

2 个答案:

答案 0 :(得分:3)

你的意思是,如何使它们相互排斥?为它们分配相同的“名称”属性。

答案 1 :(得分:3)

为了对无线电进行分组,他们需要共享共同的name。然后,check属性由浏览器

管理

HTML直接从jQueryUI radio demo

复制
<input type="radio" id="radio1" name="radio"><label for="radio1">Choice 1</label>
<input type="radio" id="radio2" name="radio" checked="checked"><label for="radio2">Choice 2</label>
<input type="radio" id="radio3" name="radio"><label for="radio3">Choice 3</label>