jquery:如何选择嵌套的radio元素

时间:2013-10-15 13:46:13

标签: jquery

HTML代码:

<div class="poll_all_chs" >
            {% for ch in poll_obj.get_choices %}
                <div class="poll_ch">
                    <span><input type="radio" id="radio_button" name="choice_id" value="{{ch.id}}">{{ch.choice}}</span>
                </div>
</div>

我想使用jquery来检查单选按钮的状态,如

if(radio_ele).is(':checked')) { 
                        bool =  true
                }

但是  我怎样才能访问单选按钮元素?

3 个答案:

答案 0 :(得分:1)

你可以这样做:

$(".poll_ch span :radio").each(function() {
    if (this.checked)
        console.log("Im checked!");
});

另外,重要提示,您将使用您提供的代码重复ID。解决这个问题,这都是坏消息。

答案 1 :(得分:0)

if($('#radio_button').is(':checked')){
   //code here for checked 
}

使用以下代码

ID必须是唯一的。阅读Two HTML elements with same id attribute: How bad is it really?

if($('input:radio[name="choice_id"]').is(':checked')){
   //code here for checked 
}

$('input:radio[name="choice_id"]').each(function() {
    if (this.checked){
       //code here for checked 
    }
});

答案 2 :(得分:0)

试试这个

$(".poll_ch :radio").each(function() {
    if ($(this).prop('checked')==true)
        console.log("This radio checked");
});
相关问题