简体   繁体   中英

Trouble with Variable value in function

I have the following script where a variable gets its value from an input field, however when I run my function its not working, returns nothing. Im new to JS so im unsure if it needs to be part of a function *even though Ive tried this with no luck) or what...

/////////////////////////////////////////////////////////////////////

// Variables

// Content/SLA
var ContentMinutes = '';
var ContentMinutesSelector; // Switch Case
var ServiceLevel = 5;
var NoOfFrames = 2;

// Render Time (Hairier the Better)
var AvgFrameRenderTime = '';
var AvgFrameRenderTimeSelector = 10; // Switch Case
var CoresInTest = document.getElementById('CoresInTest').value;

// Other
var EstimatedCoreHours = NoOfFrames * CoresInTest * AvgFrameRenderTimeSelector;

// Cost Estimate
var CostEstimate = ServiceLevel * EstimatedCoreHours;


/////////////////////////////////////////////////////////////////////

//  Functions

function CalculateEstimate() {  
// Estimate Cost
parseInt(document.getElementById("PriceEstimate").innerHTML=CostEstimate.toFixed(2));

// Estimate Core Hours
parseInt(document.getElementById("EstimatedCoreHours").innerHTML=EstimatedCoreHours.toFixed(    2));
}

my PriceEstimate and EstimatedCoreHours fields are both just empty divs, <div id="EstimatedCoreHours"></div> , My calculations work if i define a value for the variable as opposed to document.getElementById so I believe I must need to run a function or something to update all the vartiables?

But if I set...

var CoresInTest = document.getElementById('CoresInTest').value;

to

var CoresInTest = 10;

Then it works fine...

Its not actually my return, the problem is my variables arent calling, IF i define them with a number then it works.

I guess you need to do something like this, if you are looking to get calculated data in your div.

document.getElementById("PriceEstimate").innerHTML=parseInt(CostEstimate.toFixed(2));    
// Estimate Core Hours
document.getElementById("EstimatedCoreHours").innerHTML=parseInt(EstimatedCoreHours.toFixed(2));

If var CoresInTest = 10; works fine, then your code is placed wrong.

What element is CoresInTest ? is it a text field? and if so is this script placed or called before the element renders? then you will have to reinitialize that variable.

If PriceEstimate and EstimatedCoreHours are elements you should use the value property this might work for you:

document.getElementById("PriceEstimate").value=parseInt(CostEstimate.toFixed(2),10);    
document.getElementById("EstimatedCoreHours").value=parseInt(EstimatedCoreHours.toFixed(2),10);

If var CoresInTest = 10; makes it work fine, then it must be something to do with document.getElementById('CoresInTest').value - so why isn't that working? Is it a drop down list? Instead of us guessing, tell us.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM