javascript 获取单选按钮的值 发表于 2019-03-18 更新于 2022-04-04 分类于 JavaScript , DOM Waline: 本文字数: 1.5k 阅读时长 ≈ 1 分钟 测试代码1234567891011121314151617181920212223242526272829303132333435<div onchange="show();"> <input type="radio" name="fruits" value="Apple" checked="checked" />Apple <br> <input type="radio" name="fruits" value="Banana" />Banana <br> <input type="radio" name="fruits" value="Strawberry" />Strawberry <br> <input type="radio" name="fruits" value="watermelon" />watermelon <br> <input type="text" id="show"> <br></div><script> function show() { document.getElementById('show').value = '你选择了: ' + getRadioValueByName('fruits'); } // 根据单选按钮的name属性来获取这一组单选按钮的值 function getRadioValueByName(name) { var value = ""; //某个单选按钮组 var radio = document.getElementsByName(name); //遍历该组的单选按钮 for (var i = 0; i < radio.length; i++) { //判断用户选了哪个按钮 if (radio[i].checked == true) { //找到 value = radio[i].value; //不用找了 break; } } //返回选择的值 return value; } </script> 运行效果AppleBananaStrawberrywatermelon