繁体   English   中英

值单选框可使用javascript输入文本

[英]Value radio boxes to input text with javascript

我尝试从输入框中的单选框中获取值

<form action="test()">
    <input type="radio" name="typ" id="typ" value="ws" onfocus="test()" checked> ws
    <input type="radio" name="typ" id="typ2" value="nb" onfocus="test()"> nb
    <input type="radio" name="typ" id="typ3" value="za" onfocus="test()"> za
    <input type="radio" name="typ" id="typ4" value="zb" onfocus="test()"> zb
</form>

这是我的js:

<script type="text/javascript">

    function test(){
        document.getElementById('serverid').value=document.getElementById('typ').value;
    }

    function test(){
        document.getElementById('serverid').value=document.getElementById('typ2').value;
    }

    function test(){
        document.getElementById('serverid').value=document.getElementById('typ3').value;
    }

    function test(){
        document.getElementById('serverid').value=document.getElementById('typ4').value;
    }

</script>

这是我的输入框,在这里我将获得单选框的值:

<input type="text" style="background-color:#ffffff" size="15" name="ID" id="serverid">

但是,每次输入框中都是zb,但是我会选择:-)我希望您能理解我,我很抱歉我的英语。

我希望有一个人可以帮助我。

最后一个“测试”功能将隐藏所有先前的“测试”。 为它们命名不同,或使用onfocus =“ test('ws / nb / za / zb')”和

function test(rbId)
{
  document.getElementById('serverid').value=document.getElementById(rbId).value;
}

您只需要一个功能:

function test() {
    r = document.forms['theform'].elements['typ'];
    for(var x = 0; x < r.length; x++) {
            if(r[x].checked) {
                radiovalue = r[x].value;
            }
        }
    document.getElementById('serverid').value=radiovalue;
}

用表格名称替换表格

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM