我正在尝试删除文本字段中可能存在的任何内容。这些文本字段具有不同的 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/