Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中单击任意按钮获得这些值_Javascript - Fatal编程技术网

如何在javascript中单击任意按钮获得这些值

如何在javascript中单击任意按钮获得这些值,javascript,Javascript,如何在javascript中使用document.getElementById <input type="radio" name="rbRate" id="1" value="1"/>1 <input type="radio" name="rbRate" id="2" value="2"/>2 <input type="radio" name="rbRate" id="3" value="3"/>3 <input type="radio" name

如何在javascript中使用
document.getElementById

<input type="radio" name="rbRate" id="1" value="1"/>1 
<input type="radio" name="rbRate" id="2" value="2"/>2 
<input type="radio" name="rbRate" id="3" value="3"/>3 
<input type="radio" name="rbRate" id="4" value="4"/>4 
<input type="radio" name="rbRate" id="5" value="5"/>5 
1
2.
3.
4.
5.

您可以通过
document.getElementById
获取对它们的引用,然后从它们的
属性中获取它们的值。但请注意,无论是否选中该按钮,这将为您提供分配给单选按钮的
值。(为此,请查看它的
checked
属性。)

例如:

var n, checkedValue, rdo;

for (n = 1; n <= 5; ++n) {
    rdo = document.getElementById(n); // n will get promoted to a string automatically
    if (rdo.checked) {
        checkedValue = rdo.value;
        break;
    }
}
var n,checkedValue,rdo;

对于(n=1;n使用
value
属性,如
document.getElementById('1')。value