2011-05-24 46 views
0

我有以下腳本,其中一個變量從輸入字段獲取其值,但是當我運行我的函數時它不工作,不會返回任何內容。進出口新的JS這樣的IM不能確定,如果它需要一個功能的一部分*儘管我用盡這沒有運氣好的話),或者什麼...函數中變量值的問題

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

// 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)); 
} 

我PriceEstimate和EstimatedCoreHours場均只有空白的div元素,<div id="EstimatedCoreHours"></div> ,我的計算工作,如果我爲變量定義一個值而不是document.getElementById,所以我相信我必須運行一個函數或更新所有vartiables?

但如果我設置...

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

var CoresInTest = 10; 

然後正常工作......

,它不一定是我的回報,這個問題是我的變量調用的arent ,如果我用一個數字來定義它們,那麼它就可以工作。

+1

如果'PriceEstimate'或'EstimatedCoreHours'是輸入字段(''),則應該使用'.value'而不是'.innerHTML'。 – 2011-05-24 07:43:52

+0

我注意到的一些東西,你在調用parseInt,但是你沒有把它返回的值。而你的方法實際上沒有任何返回語句,所以它不會返回任何東西。 – JohnP 2011-05-24 07:46:11

回答

1

我猜你需要做這樣的事情,如果你正在尋找在你的div來獲得計算出的數據。

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

如果var CoresInTest = 10;工作正常,那麼你的代碼放在錯誤的。

什麼元素是CoresInTest?它是一個文本字段?如果是的話,這個腳本在元素呈現之前放置或調用?那麼你將不得不重新初始化該變量。

+0

不知道有什麼變化。不知道爲什麼作者將'parseInt'和'toFixed'放在一起。要求是整數或2位小數,我猜它是2位小數。 – 2011-05-24 08:13:52

0

如果PriceEstimate和EstimatedCoreHours的元素,你應該使用的價值屬性 這可能會爲你工作:

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

問題說他們是DIV元素。不是表單域。 – 2011-05-24 08:14:51

0

如果var CoresInTest = 10;使得它做工精細,那麼它必須是與document.getElementById('CoresInTest').value - 那麼,爲什麼ISN那工作嗎?它是一個下拉列表嗎?不要猜測,告訴我們。