获得Radio选中的后面文本框的值

例如:

选中的radio的值在button中显示

代码如下:

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript">

        window.onload = function () {
            document.getElementById("btn").onclick = function () {
                var phone = ""
                var rdos = document.getElementsByName("rdo");
                for (var i = 0; i < rdo.length; i++) {
                    if (rdos[i].checked) {
                        phone = document.getElementById(rdos[i].id + "_demo").value;
                        document.getElementById("btn").value = phone;
                    }
                }
            }
        }
    </script>
</head>
<body>
    移动:
    <input type="radio" name="rdo" id="mobile" />
    <input type="text" name="name" id="mobile_demo" />
    固话: 
    <input type="radio" name="rdo" id="tel" />
    <input type="text" name="name" id="tel_demo" />
    <input type="button" name="name" value="您选择的号码是?" id="btn"/>
    </body>
</html>

值得注意的是radio的值和其后文本框的值命名有一定关系。这样可以通过选中的radio的值得到其后文本框的值。

posted @ 2013-07-30 16:16  卡西法儿  阅读(942)  评论(0编辑  收藏  举报