2011-04-03 25 views
0

我有3 TextFields,稱爲txtUSD,txtEUR,txtAUS。和一個PopupList具有相同的值,減去txt部分,但我需要根據用戶所做的選擇形成要使用的名稱TextFields。所以,我已經做到了這一點:變量和對象的問題

function btConvert_Click(event) 
{ 
    var amount = document.getElementById("txtAmount").value; 
    var rates = document.getElementById("lstConvertTo").value; 
    var from = "txt" + document.getElementById("lstFrom").options[document.getElementById('lstFrom').selectedIndex].text; 
    var to = "txt" + document.getElementById("lstConvertTo").options[document.getElementById("lstConvertTo").selectedIndex].text; 
    var curr_from = document.getElementById(from).value; 
    var curr_to = document.getElementById(to).value; 

    if(curr_from > curr_to) 
    { 
     amount * rates; 
    } else { 
     amount/rates; 
    } 
    alert(result); 
} 

但每次我嘗試它的時候我得到這個錯誤:

mobile/main.js line 215: Result of expression 'document.getElementById(from)' [null] is not an object.

我應該怎麼做呢?

+0

你有ID爲'from'和'to'的元素嗎? – bensiu 2011-04-03 02:16:09

+0

O,男孩純JavaScript非常痛苦:O – arma 2011-04-03 02:17:05

+0

他們都是'value','from'和'to'的值應該是'txt'的組合+所選值 – 2011-04-03 13:02:41

回答

1

從你得到的錯誤,它看起來像生成from變量時有一個錯誤。

爲了簡潔,您應該考慮將document.getElementById('lstFrom')存儲到它自己的變量中。