<%@ Page Language="C#" AutoEventWireup="true" CodeFile="myjavascript.aspx.cs" Inherits="myjavascript" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">        var theDayElement = window.document.form1.theDay;/////错误: 'window.document.form1.theDay' 为空或不是对象
        document.write("Theree are" + theDayElement.length + "options<br>");
        var optionCounter;
        for (optionCounter = 0; optionCounter < theDayElement.length; optionCounter++) {
            document.write("Option text is" + theDayElement.options[optionCounter].text)
            document.write(" and its value is ");
            document.write(theDayElement.options[optionCounter].value);
            document.write("<br>")
        }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

解决方案 »

  1.   

    要先定义在调用啊
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="myjavascript.aspx.cs" Inherits="myjavascript" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title></title>
      
    </head>
    <body>
      <form id="form1" runat="server">
      <div>
       
      </div>
      </form>
    </body>
    </html>
    <script type="text/javascript">  var theDayElement = window.document.form1.theDay;
      document.write("Theree are" + theDayElement.length + "options<br>");
      var optionCounter;
      for (optionCounter = 0; optionCounter < theDayElement.length; optionCounter++) {
      document.write("Option text is" + theDayElement.options[optionCounter].text)
      document.write(" and its value is ");
      document.write(theDayElement.options[optionCounter].value);
      document.write("<br>")
      }
    </script>
      

  2.   

    你的js代码也没包在function里所以直接执行了,这时候还没加载到body当然要报错
      

  3.   


    window.onload = function(){
            var theDay   =    document.getElementById("theDay");
            }这样比较好 。