Javascript If具有布尔值的语句

时间:2013-06-27 19:23:18

标签: javascript

我有两个下拉选择框,我希望文本框更新为基于两个框中所选文本的值。 如果选中的第一个框是'美元'而第二个框是'naira',我希望费率为100,并在文本框中输入此值,其id为rate。

不幸的是,当我的函数执行时,我一直得到一个未定义的值。我的if语句显然有些错误,但我无法弄清楚。我需要用纯javascript而不是jquery来做这个。

这是我的代码:

<p>Select currency to send</p>  
<select id="fromCurrency">
<option value = "Dollars">Dollars</option>  
<option value = "Pounds">Pounds</option>
<option value = "Naira">Naira</option>
</select>

<p>Select currency to receive</p>
<select id="toCurrency">
<option value = "Naira">Naira</option>
<option value = "Dollars">Dollars</option>  
<option value = "Pounds">Pounds</option>

</select><br />
<label>Enter amount to send</label>
<input type="text" id="amount" name="amount"><br />
<button onclick ="getRate()">Get Rate</button><br />
<label>Rate:</label>
<input type="text" id="rate" name="rate"><br />
<label>Total:</label>
<input type="text" id="total" name="total"><br />



<script>

function getRate() {
var e = document.getElementById("fromCurrency");
var eSend = e.options[e.selectedIndex].value;
var i = document.getElementById('toCurrency');
var eReceive = i.options[i.selectedIndex].value;
var rate = document.getElementById('rate');

var dollars = {
'pounds':20,
'naira':15
};


if (eSend =='dollars' && eReceive =='naira') {
var rValue= (dollars['pounds']);
};  
rate.value = rValue;

};
</script>

请指教。 感谢

编辑: 我尝试使用switch语句,因为我有大约6种不同的条件,这就是我尝试过但它没有用。

var rValue = 0;

switch (rValue) {
case (eSend =='Dollars' && eReceive =='Naira'):
rValue= (dollars['naira']);
break;
case (eSend =='Dollars' && eReceive =='Pounds'):
rValue= (dollars['pounds']);
break;
}

rate.value = rValue;

1 个答案:

答案 0 :(得分:5)

JavaScript区分大小写,您的if语句:

if (eSend =='dollars' && eReceive =='naira') {

正在寻找dollars,而不是Dollars,大写以匹配您的select值:

if (eSend =='Dollars' && eReceive =='Naira') {