【问题标题】:Value radio boxes to input text with javascript值单选框以使用 javascript 输入文本
【发布时间】:2010-07-02 22:05:46
【问题描述】:

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

<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,但我会选择:-)我希望你能理解我,我很抱歉我的英语。

我希望有人可以帮助我。

【问题讨论】:

    标签: javascript input radio-button


    【解决方案1】:

    最后一个“test”函数隐藏了所有前面的“test”。以不同的方式命名它们,或使用 onfocus="test('ws/nb/za/zb')" 和

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

    【讨论】:

      【解决方案2】:

      你只需要一个函数:

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

      将表单替换为您的表单名称

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-03
        • 1970-01-01
        • 1970-01-01
        • 2015-04-17
        • 1970-01-01
        • 2016-10-07
        相关资源
        最近更新 更多