JavaScript 错误 "hasText(...) is not a function"

标签 javascript

我有以下 JavaScript,它似乎声明了一个简单的本地函数 hasText,然后稍后调用该函数。

function hasText(addrElem) {
    if (addrElem.value === "") {
        return false;
    }
    else {
        return true;
    }
}

var addr = [
    document.getElementById('AccountRequest_AddressLine1'),
    document.getElementById('AccountRequest_City'),
    document.getElementById('AccountRequest_Country'),
    document.getElementById('AccountRequest_State'),
    document.getElementById('AccountRequest_PostCode')
];

// Add a listener to each Address item which waits for its text box to go out of focus
addr.forEach(function (addrElem) {
    addrElem.onblur = function () {
        // Check whether or not each of the Address text boxes have text in them
        if (addr.every(this.hasText(addrElem))) {
            alert("Every text box has text!");
        }
    };
});

但是,当我在 Chrome 中运行此代码时,出现以下错误:

TypeError: hasText(...) is not a function

有人可以向我解释一下为什么我在调用 hasText 作为函数时遇到问题,并演示如何解决这个问题吗?

谢谢!

最佳答案

hasText 是一个全局函数,你必须这样调用它:

if (addr.every(hasText)) {
   ...
}

干杯。

关于JavaScript 错误 "hasText(...) is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49582610/

相关文章:

javascript - bool 表达式 - 与 AND、OR 逻辑运算符混淆以及它们的工作原理

javascript - 在 JS 中将纸张切割成最小数量的正方形

javascript - 如何使用 mootools 确定损坏图像的错误代码

javascript - HTML 单元由于某种原因无法找到表单。我怀疑是因为 angularJS 但不确定

javascript - 鼠标悬停的触摸事件

javascript - 如何使用javascript/jquery清除<table>标签的特定<td>数据?

javascript - 如何从数组中删除满足条件的元素

javascript - GoogleMap JSX 组件中的 undefined variable

javascript - 使函数仅在事件/聚焦的输入/按钮/文本区域上工作的 jQuery 或 javaScript 语法是什么?

javascript - Google map 地点 ID 查找器 - 无法在 InfoWindow 上突出显示(复制)信息