Could not able to get checked child radio button

时间:2015-09-14 15:29:15

标签: jquery jquery-selectors radio-button

I have radio buttons sets inside containers with class named answers the container may be a span or ol i.e:

<ol class="answers"> <!-- radio buttons set here li per each button --></ol>

OR
<span class="a-panel answers"><!-- radio buttons set here --></span>

All radio sets per each container have unique attribute name value.

I could not able to get the checked value using the following code:

$(".answers").each(function(){
alert($(this).children("input[type=radio]:checked").val())
})

It always alert undefined. This is a Jsbin DEMO

1 个答案:

答案 0 :(得分:1)

This is a pure javascript solution. Give all your radios the same name and different id. Then get the id with this function:

function obtainSelectedRadioId(name) {
    var radios = document.getElementsByName(name);
    var res = '';
    for (var i = 0, length = radios.length; i < length; i++) 
        if (radios[i].checked) 
            res = radios[i].id;
    return res;
}