请问如何判断radio按钮是否被选中?
我下面的代码对吗?如果不对的话请帮助我指出那里错了
谢谢了?
我是想把选中的按钮的value值显示在text里面
<script> 
function showRadio()
{
    if(x.a.checked==true)
    {
      text.value=x.a.value;
     }
}
</script> 
<form name="x" >
<input type="radio" value="喜欢"  name="a">喜欢
<br>
<input type="radio" value="不喜欢"  name="a"  >不喜欢
<br>
<input type="text"   name="text" >
<br>
<input type="button"   name="c"    value="确定" onclick="showRadio()">
</form>

解决方案 »

  1.   

    <script> 
    function showRadio()
    {
        if(document.all.x.a[0].checked==true)
        {
          document.all.x.text.value=document.all.x.a[0].value;
         }
    if(document.all.x.a[1].checked==true)
        {
          document.all.x.text.value=document.all.x.a[1].value;
         }
    }
    </script> 
    <form name="x" >
    <input type="radio" value="喜欢"  name="a" onclick="showRadio()">喜欢
    <br>
    <input type="radio" value="不喜欢"  name="a"  onclick="showRadio()">不喜欢
    <br>
    <input type="text"   name="text" >
    <br>
    <input type="button"   name="c"    value="确定" />
    </form>
      

  2.   

    document.all指文档内所有的标签然后再.x定位到你的from标签,然后再.a定位到你的input type=radio标签,但由于你有两个同名的标签,所以要用下标[0]、[1]来区分document.all.x.text也是这样,因为text被包含在x中,所以这样定位你也可以使用document.getElementsByName('元素的name')来定位,如果有多个同名元素,还要用下标区分。或者为元素加上id="xxx"属性,直接用document.getElementById('xxx')来定位,注意id是不允许同名的。
      

  3.   

    <script> 
    function showRadio()
    {
     if(document.getElementById("radio_1").checked == true){
      document.x.text.value = document.getElementById("radio_1").value;
     }
     else{
      document.x.text.value = document.getElementById("radio_2").value;
     }
    }
    </script> 
    <form name="x" >
    <input type="radio" id="radio_1" value="喜欢"  name="a">喜欢
    <br/>
    <input type="radio" id="radio_2" value="不喜欢"  name="a"  >不喜欢
    <br/>
    <input type="text"   name="text" >
    <br/>
    <input type="button"   name="c"    value="确定" onclick="showRadio()">
    </form>