javascript - 未捕获的类型错误 : Cannot set property 'value' of null even though it is not null

标签 javascript

我正在尝试删除文本字段中可能存在的任何内容。这些文本字段具有不同的 ID,并且 ID 需要从计数中生成。所以我知道 id 已经创建并且存在。这是我的 js 代码。

    var score= 2;
    function hideNewDiv(){

        document.getElementById('showAnotherDiv').style.display='inline';
        score--;
        document.getElementById("Feedback_"+score+"_score").value = "";
        document.getElementById('newSF' + score).style.display='none';

        if (score== 2){
            document.getElementById('hideAnotherDiv').style.display='none';
        }
    }

当它要将值设置为“”时,我得到了错误。 任何帮助将不胜感激

提前致谢。

最佳答案

这意味着不存在 ID 为 "Feedback_"+score+"_score" 的元素。 score 从 2 开始,但每次运行函数时都会递减它,因此它变为 1,然后是 0,然后是 - 1,依此类推,取决于你调用了多少次。在尝试使用它访问元素之前,您需要验证 score 的值。

关于javascript - 未捕获的类型错误 : Cannot set property 'value' of null even though it is not null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21942150/

相关文章:

javascript - View 不在 ASP.NET MVC 中呈现 CSS 和 Javascript

javascript - 如何使用 JavaScript 创建文件?

javascript - 如何更改 anchor 标记的属性值

javascript - 无法在 SelectBoxIt 中制表

javascript - 极其烦人的 JavaScript 数组/对象错误

javascript - 如果文本区域为空,如何使按钮禁用

javascript - Bootstrap 数据表自定义下拉过滤器

javascript - IE9 报告缺少存在的分号

javascript - 如何在 Svelte/Sapper 中加载外部 JS 库?

php - 本地从浏览器静默打印 PDF 到特定打印机