使用jQuery获取单选按钮组的值 [英] get the value of radio button groups using jquery

查看:275
本文介绍了使用jQuery获取单选按钮组的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php循环代码来生成单选按钮组

I have a php loop code to generate radio button group

<?php
for($i=1; $i<=10; $i++)
{
?>           
<div class="radio">
    <label>
        <input name="ans_<?php echo $i?>" value="option2" type="radio">
        The smallest resistance
    </label>
</div>
<div class="radio">
    <label>
        <input name="ans_<?php echo $i?>" value="option2" type="radio">
        The largest resistance
    </label>
</div>
<div class="radio">
    <label>
        <input name="ans_<?php echo $i?>" value="option2" type="radio">
        They have the same power loss.
    </label>
</div>
<div class="radio">
    <label>
        <input name="ans_<?php echo $i?>" value="option2" type="radio">
        Voltage and resistance values are needed.
    </label>
</div>
<?php
}
?>
<button id="q_next" type="button" class="btn btn-warning pull-right savebtn" name="1">Save and Next</button>

当我们单击此按钮时,名称属性值增加1

when we click on this button the name attribute value increases by 1

我正在使用此名称属性来制作单选按钮的名称.

I am using this name attribute to make the name of the radio button.

现在,我希望jquery获取通过单击按钮生成的每个组的选定单选的值??

Now I want the jquery to get the value of selected radio of each group generated on the click of the button???

我尝试了这个,但是没有用

I tried this but its not working

$('#q_next').click(function() { 
  $quesid=parseInt($(this).attr('name')); 
  $ans=$("input:[name='ans_'"+$quesid+"]").val(); 
});

推荐答案

尝试类似的方法

var radio_groups = {};
$(":radio").each(function(){
    radio_groups[this.name] = true;
})

for(group in radio_groups){
    var selected = $(":radio[name="+group+"]:checked")
    if (selected.length > 0)
        alert('GROUP : '+ group + ',VALUE : '+ selected.val());
}

这篇关于使用jQuery获取单选按钮组的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆