简体   繁体   English

JS 错误:预期对象

[英]JS error: object expected

I load an internal page that I developed in IE and at the bottom it displays some JS error: It says Line 107 character 6. I look at the JScript file and it has this code:我加载了我在 IE 中开发的内部页面,在底部显示了一些 JS 错误:它显示第 107 行字符 6。我查看 JScript 文件,它有以下代码:

 function isLessThanStartDate(obj)
    {
     var startdate = new Date(document.getElementById('txtSD').value);
     var enddate = new Date(obj.value);
     var weekending = new Date(document.getElementById('txtWE').value);

     if (!(isDate(startdate)))
   {
        obj.style.backgroundColor="red";
        alert (obj.value + " is not a valid date!");
        obj.value="";
        return;
   }

     if (enddate < startdate)
      {
      obj.style.backgroundColor="red";
      alert ("End date: " + enddate + " cannot be less then start date: " + startdate);
      obj.value="";
      }
     else if (enddate > weekending)
     {
      obj.style.backgroundColor="red";
      alert ("End date: " + enddate + " cannot be greater then week ending date: " + weekending);
      obj.value="";
     }
      else
      {
      obj.style.backgroundColor="";
      }      
    }

Line 107 is the line where it says第 107 行是它说的那一行

var weekending = new Date(document.getElementById('txtWE').value);

Why is this complaining?为什么会这样抱怨? I don't see anything wrong with it...我看不出有什么不妥...

Oftentimes, an error will tell you exactly what is wrong and where.通常,错误会告诉您确切的问题和位置。 This is the case here.这里就是这种情况。

We can tell what isn't wrong on the line.我们可以在线判断什么是没有错的。

  1. First, we know we have document because we'd have larger problems if we didn't, and we'd find out two lines earlier.首先,我们知道我们有document ,因为如果我们没有,我们会遇到更大的问题,而且我们会更早发现两行。 Scratch that.刮那个。
  2. Next, we know that the value passed to the Date constructor doesn't matter.接下来,我们知道传递给 Date 构造函数的值无关紧要。 new Date("foo") will throw no error, it will just create a Date which when alerted reports "Invalid Date" . new Date("foo")不会抛出任何错误,它只会创建一个Date ,当收到警报时报告"Invalid Date" Even new Date(null) is ok.即使new Date(null)也可以。
  3. Assignment can't throw this exception.赋值不能抛出这个异常。

This leaves us with very little to check out.这让我们几乎没有什么可以检查的。 As others have suggested, document.getElementById('txtWE') must not be returning the object you're expecting.正如其他人所建议的那样, document.getElementById('txtWE')一定不能返回您期望的对象。 Knowing this, see this jsfiddle .知道这一点,请参阅这个jsfiddle If I "break" the input with id="txtWE" to be any other ID, I get your error.如果我将 id="txtWE" 的输入“打破”为任何其他 ID,我会收到您的错误。

Are you sure that txtWE is there?你确定 txtWE 在那里吗?

If your error is in IE, you can right-click on the page and View Source.如果您的错误是在 IE 中,您可以右键单击该页面并查看源代码。 Then, check THAT page for line 107. It will probably be different than that "var weending..." code.然后,检查该页面的第 107 行。它可能与“var weending...”代码不同。 Or, if it is in fact the 107 code you posted, perhaps a Date can't be created from the txtWE value.或者,如果它实际上是您发布的 107 代码,则可能无法从 txtWE 值创建日期。 Are you validating that txtWE input in any way?您是否以任何方式验证该 txtWE 输入?

From the coment on the question, the error is 'object required'.从问题的评论来看,错误是“需要对象”。 That usually means that you're trying to access a property of something that's null.这通常意味着您正在尝试访问为空的东西的属性。

Your line 107 might do that, though, if document.getElementById('txtWE') returned null.但是,如果document.getElementById('txtWE')返回 null,您的第 107 行可能会这样做。 When you run just that code, at the console, do you get an element or do you get null?当您仅在控制台上运行该代码时,您会得到一个元素还是得到 null? To put it another way, please check whether you have an element in the page whose ID (not name!) is txtWE .换句话说,请检查页面中是否有 ID(不是名称!)为txtWE的元素。

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

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