<script >
function abc() {
var obj = document.form1.getElementsByName("rd_id");
for(var i=0;i<obj.length;i++){
if(obj[i].checked){
document.form2.getElementById("txtID").value=obj[i].value;
return true;
}
}
}</script>
</head><body>
<form id="form1" name="form1" action="" method="get">
  <input type="radio" name="rd_id" value="1" onchange="abc();"  /><br />
   <input type="radio" name="rd_id" value="2" onchange="abc();"  /><br />
 <input type="radio" name="rd_id" value="3" onchange="abc();"  /><br />
  <input type="radio" name="rd_id" value="4" onchange="abc();"  /><br />
</form>
<form id="form2" name="form2" method="post" action="">
<input name="txtID" id="txtID" type="text" />
</form>
</body>我想要的结果就是哪个radiobutton被选中了,textbox就自动的添加该radiobutton的value值。
可是我这段代码运行了就是没反应,不知道哪边出错了,求高手帮忙!谢谢!

解决方案 »

  1.   

    <script >
    function abc() {
    var obj = document.getElementsByName("rd_id");
    for(var i=0;i<obj.length;i++){
    if(obj[i].checked){
    document.getElementById("txtID").value=obj[i].value;
    return true;
    }
    }
    }</script>
    </head><body>
    <form id="form1" name="form1" action="" method="get">
      <input type="radio" name="rd_id" value="1" onchange="abc();" /><br />
      <input type="radio" name="rd_id" value="2" onchange="abc();" /><br />
     <input type="radio" name="rd_id" value="3" onchange="abc();" /><br />
      <input type="radio" name="rd_id" value="4" onchange="abc();" /><br />
    </form>
    <form id="form2" name="form2" method="post" action="">
    <input name="txtID" id="txtID" type="text" />
    </form>
    </body>
      

  2.   

    getElementsByName是document的方法 
    当然楼主要缩小范围也可以
    <script >
    function abc() {
    var obj = document.getElementById("form1").getElementsByTagName("input");
    for(var i=0;i<obj.length;i++){
    if(obj[i].type == "radio" && obj[i].checked){
    document.getElementById("txtID").value=obj[i].value;
    return true;
    }
    }
    }</script>
      

  3.   

    或者document.form2.txtID.value=obj[i].value;   也可以。
      

  4.   

    那请问
    为什么我点击完radiobutton后,
    要在页面其他地方再用鼠标点击一下,radiobutton的值才会显示到textbox上?!能否在我点击radiobutton的同时,它的值就自动的显示到textbox上呢?
      

  5.   

    额不懂
    我换了,用onclick事件就好了