fmpq.com
源代码:
点击运行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Fmpq教程(fmpq.com)</title> <script> function displayResult(){ var radios = document.getElementsByName('colors'); for (var i = 0, length = radios.length; i < length; i++) { if (radios[i].checked) { // 弹出选中值 alert(radios[i].value); // 选中后退出循环 break; } } } </script> </head> <body> <form> 你更喜欢哪种颜色?<br> <input type="radio" name="colors" id="red" value="red">红色<br> <input type="radio" name="colors" id="blue" value="blue">蓝色<br> <input type="radio" name="colors" id="green" value="green">绿色 </form> <button type="button" onclick="displayResult()">显示选中的值</button> </body> </html>
运行结果