문제

What I want to do is pretty simple. Look at this form:

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

With the following script:

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

Now this code doesn't make the radio buttons mutually exclusive. If I change "radio" to "checkbox" everything works ok. But I want to understand why this doesn't work with radio input? Is this a bug with jQuery or I am missing something important?

도움이 되었습니까?

해결책

In order to group radios they need to share common name. The checked property is then managed by browser

HTML copied directly from 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>

다른 팁

Do you mean, how to make them mutually exclusive? Assign them the same "name" attribute.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top