admin管理员组

文章数量:1420073

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

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 ment 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();

    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?

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

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 ment 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();

    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?

Share Improve this question asked Dec 12, 2012 at 18:15 wowzuzzwowzuzz 1,39011 gold badges31 silver badges51 bronze badges 1
  • are you sure you have elements with those ID in your page? – wirey00 Commented Dec 12, 2012 at 18:18
Add a ment  | 

2 Answers 2

Reset to default 1

This error means that the id dt_version does not exist. Check your html to make sure it is there:

var dt =  document.getElementById("dt_version");
if (dt){
   // do your stuff
}else {
    console.log("dt does not exist")
}

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 pletely 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().

本文标签: