javascript 未捕获的类型错误:无法读取 null 的属性“值”,需要一些指导
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13846051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught TypeError: Cannot read property 'value' of null, need some direction
提问by wowzuzz
I have been trying to figure out this particular problem in my developer tools, but I've had no luck thus far. I have an error on one of my js files that says
我一直试图在我的开发人员工具中找出这个特定问题,但到目前为止我没有运气。我的一个 js 文件有一个错误,上面写着
Uncaught TypeError: Cannot read property 'value' of null
The following error refers to the 1st variable of dt_version below. The particular thing is if I comment out the first line of code. I get the same error on the following variables of offload1 and offload2. The variable is a number that I am trying to get passed over. I run this function on my body when the page loads...onload=updatetotal();
以下错误指的是下面 dt_version 的第一个变量。特别是如果我注释掉第一行代码。我在 offload1 和 offload2 的以下变量上遇到相同的错误。变量是我试图传递的数字。当页面加载时,我在我的身体上运行这个函数......onload=updatetotal();
function updatetotal() {
var dt_version = document.getElementById("dt_version").value-0;
var offload1 = document.getElementById("capacity_offload1").value-0;
var offload2 = document.getElementById("capacity_offload2").value-0;
var offload3 = document.getElementById("capacity_offload3").value-0;
}
If a run an if statement looking for document.getElementByID("dt_version");
...it defaults to false..so its not being carried over though on the previous page, I can see its input fine with the value in it. What am I missing here guys?
如果运行一个 if 语句寻找document.getElementByID("dt_version");
......它默认为 false..所以它不会在上一页被结转,我可以看到它的输入很好地包含其中的值。伙计们,我在这里错过了什么?
回答by Ibu
This error means that the id dt_version
does not exist. Check your html to make sure it is there:
此错误意味着该 IDdt_version
不存在。检查您的 html 以确保它在那里:
var dt = document.getElementById("dt_version");
if (dt){
// do your stuff
}else {
console.log("dt does not exist")
}
回答by Suvendu Shekhar Giri
Another cause for this error may be- as you are calling the javascript function on page load there is a possible chance that your control is not yet completely rendered to the page. A simple solution is just move that control to the beginning of the page. If it doesn't work then an reliable solution is, call the function inside jquery $(document).ready().
此错误的另一个原因可能是 - 当您在页面加载时调用 javascript 函数时,您的控件可能尚未完全呈现到页面上。一个简单的解决方案是将该控件移动到页面的开头。如果它不起作用,那么可靠的解决方案是调用 jquery $(document).ready() 中的函数。