【问题标题】:Uncaught TypeError: this.element.val is not a function error未捕获的 TypeError:this.element.val 不是函数错误
【发布时间】:2017-03-14 09:17:29
【问题描述】:

有人可以帮我解决这个错误吗?我不知道我在哪里做错了。我是 JavaScript 或 OOP 的新手,所以感谢你们的帮助。

这是我的代码:

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

我在第 3 行收到此错误:

未捕获的类型错误:this.element.val 不是函数。

【问题讨论】:

    标签: javascript jquery oop


    【解决方案1】:

    这里的 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 也很好。

    【讨论】:

      猜你喜欢
      • 2021-05-25
      • 1970-01-01
      • 2016-03-21
      • 2017-01-20
      • 2017-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多