简体   繁体   English

JavaScript出现“预期的对象”错误

[英]JavaScript Giving 'Object Expected' Error

I am writing some code that shows or hides a calendar based on a drop down select form. 我正在编写一些代码,这些代码根据下拉选择表单显示或隐藏日历。 I have it set to run my showHideCalendar() function as an onchange action on the drop down selection. 我已将其设置为将我的showHideCalendar()函数作为下拉选择上的onchange操作运行。 When I change the selected option I receive an "Object expected" error. 当我更改选定的选项时,我收到“期望的对象”错误。 I have included the relevant HTML and JavaScript code. 我已经包含了相关的HTML和JavaScript代码。

<script type="text/javascript" language="javascript">

function showHideCalendar(element) {
    document.getElementById("calendar").style.display = "none";        
    if (document.getElementById("dropdown").selectedIndex === 13) {
        cal5.popup();
        document.getElementById("textField").value = "";
        document.getElementById("calendar").style.display = "inline";        
    }
}

</script>

<FORM NAME='Labs' METHOD='POST' ACTION="">
<select id="dropdown" name='TimeFrame0' onchange='showHideCalendar(document.Labs.TimeFrame0)'>
    <option value='NoReorder'>Do Not Reorder</option>
    <option value='2012-06-14 08:40:39.067'>Today</option>
    <option value='2012-06-21 08:40:39.067'>1 Week</option>
    <option value='2012-06-28 08:40:39.067'>2 Weeks</option>
    <option value='2012-07-05 08:40:39.067'>3 Weeks</option>
    <option value='2012-07-12 08:40:39.067'>4 Weeks</option>
    <option value='2012-07-26 08:40:39.067'>6 Weeks</option>
    <option value='2012-07-14 08:40:39.067' selected>1 Month</option>
    <option value='2012-08-14 08:40:39.067'>2 Months</option>
    <option value='2012-09-14 08:40:39.067'>3 Months</option>
    <option value='2012-10-14 08:40:39.067'>4 Months</option>
    <option value='2012-12-14 08:40:39.067'>6 Months</option>
    <option value='2013-03-14 08:40:39.067'>9 Months</option>
    <option value='custom' id='custom'>Calendar Select</option>
</select>                    

<div id='calendar' style='display:inline'>
    <input type=text name='StartDate' value='' size=20 onchange='inputChanged()' onkeydown='inputChanged()' onblur='inputChanged()' id='textField'>
    <a href='javascript:cal5.popup();' onmousedown='inputChanged()'> <img src='/jscalendar/img/cal.gif' width='16' height='16' border='0' alt='Click Here to Pick up the date'></a>
</div>
<input type=checkbox name=SameDate value='on' checked onClick='SetTimeFrame(document.Labs.TimeFrame0)'> Use Same Date For All Labs
<input class='btnsave' type=button name=Save value='Reorder Labs' onClick=javascript:document.Labs.action='/LabReview/Reorder.php?PROV=PROVID&MaxCount=2&Text=1';document.Labs.submit();>
<input class='btncancel' type=button name=Cancel value=Cancel onClick=javascript:top.window.close()>      
</FORM>

Does anyone see any reason why this is not working. 有谁看到任何不起作用的原因。 The error is thrown on the line <select id="dropdown" name='TimeFrame0' onchange='showHideCalendar(document.Labs.TimeFrame0)'> . 在行<select id="dropdown" name='TimeFrame0' onchange='showHideCalendar(document.Labs.TimeFrame0)'>上引发错误。

One of the answerers thinks the error isn't in my new code, but rather something else on the page. 一位回答者认为错误不在我的新代码中,而是页面上的其他错误。 I am editing my question to add the rest of the javascript that is on the page. 我正在编辑我的问题,以添加页面上其余的javascript。

<script type="text/javascript" language="javascript">


    function showHideCalendar(element) {
        document.getElementById("calendar").style.display = "none";        
        if (document.getElementById("dropdown").selectedIndex === 13) {
            cal5.popup();
            document.getElementById("textField").value = "";
            document.getElementById("calendar").style.display = "inline";        
        }
    }

    function inputChanged() {
        document.getElementById("dropdown").selectedIndex = 13;
    }

   function SetTimeFrame(element)
     {       
       if (document.Labs.SameDate.checked==true)
         {
           var Elements=document.Labs.elements.length;
         for (var i=0; i<Elements; i++)
           {
          if (document.Labs.elements[i].type=='select-one')
              {                      
                   document.Labs.elements[i].options[element.selectedIndex].selected=true;
              }        
         }    
         }
     }

   if (top.window.opener.document.getElementById("Complete<?php echo($_GET['Item']); ?>"))
     {
       if ('<?php echo($Reviewed); ?>' == 'yes')
          {
                window.opener.document.getElementById("Complete<?php echo($_GET['Item']); ?>").style.visibility='visible';       
                 window.opener.document.getElementById("Partial<?php echo($_GET['Item']); ?>").style.visibility='hidden';       
        }
          else if ('<?php echo($Reviewed); ?>' == 'partial')
         {
                   window.opener.document.getElementById("Partial<?php echo($_GET['Item']); ?>").style.visibility='visible';       
                    window.opener.document.getElementById("Complete<?php echo($_GET['Item']); ?>").style.visibility='hidden';       
        }
   }    
     else
     {
              var temp=top.window.opener.location.href;
             top.window.opener.location.href=temp;    
             top.window.opener.top.window.location.reload(); 
     }

function DateCheck(element)
{
  var xx = element.value;
  var re = new RegExp("^[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]$");
  var re2 = new RegExp("^[0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]$");
    var re3 = new RegExp("^[0-9][0-9]/[0-9]/[0-9][0-9][0-9][0-9]$");
    var re4 = new RegExp("^[0-9]/[0-9]/[0-9][0-9][0-9][0-9]$");

    var error = "";
  var maxdays = new Array(0,31,29,31,30,31,30,31,31,30,31,30,31);
  var retval = true;
  if(element.value != "")
  {
    if(xx.search(re) < 0 && xx.search(re2) < 0 && xx.search(re3) < 0 && xx.search(re4) < 0)
    {
    error = "Invalid Date Format " + xx + ". Please enter dates in the format mm/dd/yyyy."
    retval = false;
    element.value = ""
    }
    else
    {
    var list = xx.split("/");
    var month = list[0];
    if(month.charAt(0) == "0")
    month = month.substr(1);
    if( list[0] < 1 || list[0] > 12)
    {
        error = "Invalid Month " + list[0];
        retval = false;
        element.value = ""
    }
    else 
    if(list[1] < 1 || list[1] > maxdays[month])
    {
        error = "Invalid Day " + list[1];
        retval = false;
        element.value = ""
    }
    else 
    if(list[2] < 1900)
    {
        error = "Invalid Year (Must be greater than 1900) " + list[2];
        retval = false;    
        element.value = ""
    }
    }
    if(!retval)
    alert(error);

    return retval;
  }
}



</script>

Change: 更改:

<select id="dropdown" name='TimeFrame0' onchange='showHideCalendar(document.Labs.TimeFrame0)'>

To: 至:

<select id="dropdown" name='TimeFrame0' onchange='showHideCalendar(this)'>  

It turns out the problem was caused by the following block of code. 原来问题是由以下代码块引起的。

    if (top.window.opener.document.getElementById("Complete<?php echo($_GET['Item']); ?>"))      {        
if ('<?php echo($Reviewed); ?>' == 'yes')
           {
                 window.opener.document.getElementById("Complete<?php echo($_GET['Item']); ?>").style.visibility='visible';
                         window.opener.document.getElementById("Partial<?php echo($_GET['Item']); ?>").style.visibility='hidden';
                }
           else if ('<?php echo($Reviewed); ?>' == 'partial')
          {
                    window.opener.document.getElementById("Partial<?php echo($_GET['Item']); ?>").style.visibility='visible';
                            window.opener.document.getElementById("Complete<?php echo($_GET['Item']); ?>").style.visibility='hidden';
                }
    }
          else
      {
               var temp=top.window.opener.location.href;
              top.window.opener.location.href=temp;
                  top.window.opener.top.window.location.reload();
       }

When I removed that from the code, it worked like a charm. 当我从代码中删除它时,它就像一个魅力。 Thanks to everyone for their suggestions and thoughts. 感谢大家的建议和想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM