document.form1.age[0].selected == true

解决方案 »

  1.   

    function validate () {
       for (i = 0;i<=form1.age.length;i++) {
          if(form1.age[i].checked) {
         alert(form1.age[i].value)
     }
       }
    }
    </script>
    <h1 align="center">第三题</h1>
    <form name="form1" method="post" onsubmit="return validate();">
    请选择年龄:
    <input type="radio" name="age" value="对不起,您未满18岁,没有权利投票!" checked="checked" />年龄小于18岁
    <input type="radio" name="age" value="请慎重投下您宝贵的一票!" />年龄大于等于18岁
    <br />
    <input type="reset" value="重填" />
    <input type="submit" value="提交" />
    </form>
    我采用另外的一种方式将这个功能给实现了...
    但是我发现我上面的这段代码只在IE浏览器中有作用,在firefox中就没作用了!!这又是怎么回事呢?
    请各位高人指点!!
      

  2.   

    楼主请看你的代码:
    if (document.form1.age[0].value = true) {  //应该是if (document.form1.age[0].value == "option1")
    document.form1.age[0].value 原来值是"option1",将true赋值给document.form1.age[0],document.form1.age[0].value的值为true。而在执行document.form1.age[0].value = true这条语句的时候,返回的结果始终是true,所以不管怎么运行,都会进入第一个if,而不执行else if后面的条件判断。
      

  3.   

    <script language="javascript" > 
    function validate () { 
        
       if (document.form1.age[0].value == "option1") { 
          alert("对不起,您未满18岁,没有权利投票!"); 
      //return true; 
       } 
       else if(document.form1.age[1].value == "option2") { 
          alert("请慎重投下您宝贵的一票!"); 
      //return true; 
       } 
        
       //return true;      

    </script > <h1 align="center" >第三题 </h1 > 
    <form name="form1" method="post" onsubmit="validate();" > 
    请选择年龄: 
    <input type="radio" name="age" value="option1" / >年龄小于18岁 
    <input type="radio" name="age" value="option2" / >年龄大于等于18岁 
    <br / > 
    <input type="reset" value="重填" / > 
    <input type="submit" value="提交" / > 如你所说修改,但还是不行哦...
      

  4.   

    刚才也没注意到这个问题,document.form1.age[0].value == "option1"当然返回true了,document.form1.age[0].value本来就是"option1"。这样:
    function validate () { 
        alert(document.form1.age[0].value);
    alert(document.form1.age[1].value);
       if (document.form1.age[0].checked) { 
    alert("对不起,您未满18岁,没有权利投票!"); 
    return true; 
       } 
       else if(document.form1.age[1].checked) { 
          alert("请慎重投下您宝贵的一票!"); 
      return true; 
       } 
        
       return true;      
      

  5.   

    <script language="javascript" > 
    function validate () {  
       if (document.form1.age[0].checked) {  
          alert("对不起,您未满18岁,没有权利投票!");  
          return true;  
       }  
       else if(document.form1.age[1].checked) {  
          alert("请慎重投下您宝贵的一票!");  
          return true;  
       }      
       return true;       

    </script > <form name="form1" method="post" onsubmit="validate();" > 
    请选择年龄: 
    <input type="radio" name="age" value="option1" / >年龄小于18岁 
    <input type="radio" name="age" value="option2" / >年龄大于等于18岁 
    <br / > 
    <input type="reset" value="重填" / > 
    <input type="submit" value="提交" / > 呵呵,谢谢了啊...终于搞掂这个问题咯....
      

  6.   

    function validate () { 
        
       if (document.form1.age[0].checked == true) { 
          alert("对不起,您未满18岁,没有权利投票!"); 
      return true; 
       } 
       else if(document.form1.age[1].checked== true) { 
          alert("请慎重投下您宝贵的一票!"); 
      return true; 
       } 
        
       return true;      

    </script > </head><body>
    <h1 align="center" >第三题 </h1 > 
    <form name="form1" method="post" onsubmit="return validate();" > 
    请选择年龄: 
    <input type="radio" name="age" value="option1" / >年龄小于18岁 
    <input type="radio" name="age" value="option2" / >年龄大于等于18岁 
    <br / > 
    <input type="reset" value="重填" / > 
    <input type="submit" value="提交" / > 
    </form > </body>
    </html>