gpt4 book ai didi

javascript - JS 错误 : object expected

转载 作者:行者123 更新时间:2023-12-03 23:26:58 25 4
gpt4 key购买 nike

我加载了一个我在 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="";
}
}

第 107 行是它所说的行

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

为什么会这样提示?我看不出有什么问题...

最佳答案

通常,一个错误会告诉您到底哪里出了问题。这里就是这种情况。

我们可以判断线路上没有什么问题。

  1. 首先,我们知道我们有文档,因为如果没有,我们会遇到更大的问题,而且我们会提前找到两行。从头开始。
  2. 接下来,我们知道传递给 Date 构造函数的值并不重要。 new Date("foo") 不会抛出任何错误,它只会创建一个 Date,当收到警报时会报告 “Invalid Date”。即使 new Date(null) 也可以。
  3. 赋值不能抛出这个异常。

这让我们几乎没有什么可检查的。正如其他人所建议的那样,document.getElementById('txtWE') 不能返回您期望的对象。知道这个,看这个jsfiddle .如果我将 id="txtWE"的输入“打断”为任何其他 ID,我就会收到您的错误。

你确定 txtWE 在那里吗?

关于javascript - JS 错误 : object expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6033546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com