寻找表单控件
document.form名.控件名如果没有生成这个控件,这句就会报错,说为空或不是对象。

解决方案 »

  1.   

    if (document.getElementById('xxx') != null){}
      

  2.   

    <form name="a">
    <input type="button" value="点我" onclick="_click();">
    </form><script>
    function _click(){
    if (document.a.b){
    alert(document.a.b.value);
    }
    }
    </script>
      

  3.   

    试了你们两个方法,都不行,if(document.getElementById('xxx') != null){}
    没有找到xxx,依然会进入if中。if (document.a.b){}
    没有找到b,依然会进入if中。
      

  4.   

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title></title>
    </script>
    <script language="javascript" type="text/javascript">
       function ccc()
       {
          if(document.getElementsByName("bbb") != null)
          {
             alert(document.getElementsByName("bbb").value);
          }
          else
          {
             alert("aaa");
          }
       }
       function ddd()
       {
          if(document.getElementsByName("bbb"))
          {
             alert(document.getElementsByName("bbb").value);
          }
          else
          {
             alert("aaa");
          }
       }
    </script>
    </head>
    <body bgcolor="#FFFFFF" marginheight=0 marginwidth=0 leftmargin=0>
    <BR>
     
          <Form name="form1" method="POST" action="">          <input type="button" onClick="ccc();" value=" ccc ">
     <input type="button" onClick="ddd();" value=" ddd ">
        </form>
    <br>
    </body>
    </html>