如何使用javascript获取单选按钮的值 [英] How to I get the value of a radio button with javascript

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

问题描述

我需要使用JavaScript获取单选按钮的值



我有一个名为选择

的广播组

 < input type =radioname =selectionid =selectionvalue =allClientschecked =checked/> All客户< br /> 
之间的范围

使用javascript将值传递给另一个页面

  onclick =do_action1( 'liveClickCampaigns', 'contract_type =' +的document.getElementById( 'contract_type')值+ '&安培; BEGINDATE =' +的document.getElementById( 'BEGINDATE')值+。 '&安培;结束日期=' +的document.getElementById( '结束日期' )。价值+ '&安培;选择1 =' +的document.getElementById( '选择1')值+ '&安培;选择=' +的document.getElementById( '选择')值, '内容');。类型= 按钮 > 

document.getElementById('selection')。value需要获取值,但它会一直给出即使选择了第二个单选按钮,我也是这个值。广播组不在表格中

解决方案

使用:

  function getRadioValue(name){
var group = document.getElementsByName(name);

for(var i = 0; i< group.length; i ++){
if(group [i] .checked){
return group [i] .value;
}
}

return'';

应用程式:

  var selectedValue = getRadioValue('selection'); 

演示:

http://www.jsfiddle.net/tqQWT/


I need to obtain the value of a radio button using javascript

I have a radio group called selection

<input type="radio" name="selection" id="selection" value="allClients" checked="checked" />All Clients<br />
          <input type="radio" name="selection" id="selection1" value="dateRange" />Date Range between 

I pass the value to another page using javascript

onclick="do_action1('liveClickCampaigns','contract_type='+document.getElementById('contract_type').value+'&beginDate='+document.getElementById('beginDate').value+'&endDate='+document.getElementById('endDate').value+'&selection1='+document.getElementById('selection1').value+'&selection='+document.getElementById('selection').value,'content');" type="button">

The document.getElementById('selection').value needs to get the value, but it keeps giving me the value of the first radio button even though the second is selected. The radio group is not within a form

解决方案

Use:

function getRadioValue(name) {
    var group = document.getElementsByName(name);

    for (var i=0;i<group.length;i++) {
        if (group[i].checked) {
            return group[i].value;
        }
    }

    return '';
}

Application:

var selectedValue = getRadioValue('selection');

Demo:

http://www.jsfiddle.net/tqQWT/

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

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