如何使用javascript获取单选按钮选择的值 [英] How to get radio button selected value using javascript

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

问题描述

I want to get radio button value using javascript.though i did getElementById but it is not showing me the value.please help





我尝试过:



我想用javascript获取单选按钮值。虽然我做了getElementById但它没有显示我的值。请帮助



What I have tried:

I want to get radio button value using javascript.though i did getElementById but it is not showing me the value.please help

推荐答案

I谷歌搜索获取单选按钮值javascript并快速找到这个



javascript - 如何获得所选单选按钮的值? - 堆栈溢出 [ ^ ]



请在提出问题之前做基础研究,如果该链接对您没有帮助,如果您自己google,还有更多。
I googled "get radio button value javascript" and quickly found this

javascript - How to get value of selected radio button? - Stack Overflow[^]

Please do basic research before asking a question, if that link doesn't help you there are many more if you google yourself.


试试这个:



Try this :

var radios = document.getElementsByName('rbtn');

for (var i = 0, length = radios.length; i < length; i++) {
    if (radios[i].checked) {
        // do whatever you want with the checked radio
        alert(radios[i].value);

        // only one radio can be logically checked, don't check the rest
        break;
    }
}


在这里查看我的示例:



Markup

See my sample here:

Markup
<input type="radio" value="1" name="choice">1</input>
<input type="radio" value="2" name="choice">2</input>
<input type="radio" value="3" name="choice">3</input>
<br/>
You selected: 
<label id="choiceLabel"></label>





JS



JS

(function (){
    var radios = document.getElementsByName('choice');
    console.log(radios);
    for(var i = 0; i < radios.length; i++){
        radios[i].onclick = function(){
            document.getElementById('choiceLabel').innerText = this.value;
        }
    }
})();


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

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