gpt4 book ai didi

javascript - 未捕获的 TypeError : this. element.val 不是函数错误

转载 作者:行者123 更新时间:2023-12-03 04:48:03 24 4
gpt4 key购买 nike

有人可以帮我解决这个错误吗?我根本不知道我在哪里做错了。我对 JavaScript 或 OOP 都很陌生,所以感谢你们的帮助。

这是我的代码:

Test.prototype.testing= function() {
var oneDate= new Date();
var anotherDate= new Date(this.element.val());
return oneDate.getTime() < anotherDate.getTime();
}

我在第 3 行收到此错误:

Uncaught TypeError: this.element.val is not a function.

最佳答案

这里的this指的是函数本身的范围,它不链接到任何元素。您可以尝试将元素的引用存储在函数外部并将其存储在变量中。

var element = document.getElementById('someID');

Test.prototype.testing= function() {
var oneDate= new Date();
var anotherDate= new Date(element.value);
return oneDate.getTime() < anotherDate.getTime();
}

我猜你正在使用jquery,但是像这样使用vanilla javascript也可以。

关于javascript - 未捕获的 TypeError : this. element.val 不是函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42781925/

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