开发者

Value radio boxes to input text with javascript

i try 开发者_运维问答to get the value from a radio box in a input box

<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>

And here is my 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>

This is my Inputbox and here i will get the value of the radio boxes:

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

But so it is everytime zb in the inputbox but i will choose :-) I hope you unterstand me, i'm sry for my english.

I hope some one can help me.


The last "test" function hides all preceding "test"'s. Name them differently, or use onfocus="test('ws/nb/za/zb')" and

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


You only need one function:

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;
}

Replace theform with the name of your form

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜