Kaip patikrinti radijo mygtuką naudojant jQuery?

Bandau patikrinti radijo mygtuką naudodamas jQuery. Štai mano kodas:

<form>
    <div id='type'>
        <input type='radio' id='radio_1' name='type' value='1' />
        <input type='radio' id='radio_2' name='type' value='2' />
        <input type='radio' id='radio_3' name='type' value='3' /> 
    </div>
</form>

Ir JavaScript:

jQuery("#radio_1").attr('checked', true);

Neveikia:

jQuery("input[value='1']").attr('checked', true);

Neveikia:

jQuery('input:radio[name="type"]').filter('[value="1"]').attr('checked', true);

Neveikia:

Ar turite kitą idėją? Ko man trūksta?

Sprendimas

Jei "jQuery" versijos yra lygios arba didesnės (>=) 1.6, naudokite:

$("#radio_1").prop("checked", true);

Versijoms, ankstesnėms nei (

Komentarai (15)

Turite padaryti

jQuery("#radio_1").attr('checked', 'checked');

Tai HTML atributas

Komentarai (0)
$("#radio_1").attr('checked', true);
//or
$("#radio_1").attr('checked', 'checked');
Komentarai (2)