2010-07-02 37 views
0

输入文本我试图让在输入框中值单选按钮使用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的输入框,但我会选择:-)我希望你unterstand我,我对我的英语很满意。

我希望有人能帮助我。

回答

1

最后一个“测试”功能隐藏了所有前面的“测试”。它们命名不同,或使用的onfocus = “测试( 'WS/NB/ZA/ZB')” 和

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

感谢:-)所以小问题^^ – Sebastian 2010-07-02 13:59:40

1

你只需要一个功能:

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

替换theform用的名字你形式

+0

这也是一个很好的!谢谢 – Sebastian 2010-07-02 14:11:36