我想从一组单选按钮中获得选定的值。

这是我的HTML:

<div id="rates">
  <input type="radio" id="r1" name="rate" value="Fixed Rate"> Fixed Rate
  <input type="radio" id="r2" name="rate" value="Variable Rate"> Variable Rate
  <input type="radio" id="r3" name="rate" value="Multi Rate" checked="checked"> Multi Rate  
</div>

这是我的js:

var rates = document.getElementById('rates').value;
var rate_value;
if(rates =='Fixed Rate'){
    rate_value = document.getElementById('r1').value;
    
}else if(rates =='Variable Rate'){
    rate_value = document.getElementById('r2').value;
    
}else if(rates =='Multi Rate'){
    rate_value = document.getElementById('r3').value;
}  

document.getElementById('results').innerHTML = rate_value;

我一直没有定义。


当前回答

按ID检查值:

var CheckedValues = ($("#r1").is(':checked')) ? 1 : 0;

其他回答

这个问题已经提出一年左右了,但我认为答案可能会有实质性的改进。我发现这是最简单和最通用的脚本,因为它检查按钮是否被选中,如果是的话,它的值是什么:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Check radio checked and its value</title>
</head>
<body>

    <form name="theFormName">
        <input type="radio" name="theRadioGroupName" value="10">
        <input type="radio" name="theRadioGroupName" value="20">
        <input type="radio" name="theRadioGroupName" value="30">
        <input type="radio" name="theRadioGroupName" value="40">
        <input type="button" value="Check" onclick="getRadioValue('theRadioGroupName')">
    </form>

    <script>
        function getRadioValue(groupName) {
            var radios = theFormName.elements[groupName];
            window.rdValue; // declares the global variable 'rdValue'
            for (var i=0; i<radios.length; i++) {
                var someRadio = radios[i];
                if (someRadio.checked) {
                    rdValue = someRadio.value;
                    break;
                }
                else rdValue = 'noRadioChecked';
            }
            if (rdValue == '10') {
                alert('10'); // or: console.log('10')
            }
            else if (rdValue == 'noRadioChecked') {
                alert('no radio checked');
            }
        }
    </script>
</body>
</html>

你也可以在另一个函数中调用这个函数,就像这样:

function doSomething() {
    getRadioValue('theRadioGroupName');
    if (rdValue == '10') {
        // do something
    }
    else if (rdValue == 'noRadioChecked') {
        // do something else
    }  
} 

多次直接调用单选按钮会给你FIRST按钮的值,而不是CHECKED按钮。我宁愿调用一个onclick javascript函数来设置一个变量,以便稍后可以随意检索,而不是循环遍历单选按钮来查看哪个按钮被选中。

<input type="radio" onclick="handleClick(this)" name="reportContent" id="reportContent" value="/reportFleet.php" >

电话:

var currentValue = 0;
function handleClick(myRadio) {
    currentValue = myRadio.value;
    document.getElementById("buttonSubmit").disabled = false; 
}

额外的好处是我可以处理数据和/或对按钮的检查做出反应(在这种情况下,启用SUBMIT按钮)。

只需使用:document.querySelector('input[rate][checked]').value

<form id="rates">
  <input type="radio" name="rate" value="Fixed Rate"> Fixed
  <input type="radio" name="rate" value="Variable Rate"> Variable
  <input type="radio" name="rate" value="Multi Rate" checked> Multi
</form>

然后……

var rate_value = rates.rate.value;

对于生活在边缘的人们:

现在有一个叫做RadioNodeList的东西,访问它的value属性将返回当前选中输入的值。这将消除首先过滤掉“已检查”输入的必要性,就像我们在许多张贴的答案中看到的那样。

示例的形式

<form id="test">
<label><input type="radio" name="test" value="A"> A</label>
<label><input type="radio" name="test" value="B" checked> B</label>
<label><input type="radio" name="test" value="C"> C</label>
</form>

要检索检查过的值,你可以这样做:

var form = document.getElementById("test");
alert(form.elements["test"].value);

证明这一点的JSFiddle: http://jsfiddle.net/vjop5xtq/

请注意,这是在Firefox 33中实现的(所有其他主要浏览器似乎都支持它)。旧的浏览器将需要一个RadioNodeList的polfyill来正常工作