gpt4 book ai didi

带有 bool 值的 Javascript If 语句

转载 作者:行者123 更新时间:2023-11-29 17:16:21 26 4
gpt4 key购买 nike

我有两个下拉选择框,我希望文本框根据从两个框中选择的文本更新一个值。如果选择的第一个框是“dollars”,第二个框是“naira”,我希望汇率为 100,并将此值输入到文本框中,id 为汇率。

不幸的是,当我的函数执行时,我一直得到一个未定义的值。我的 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;

最佳答案

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

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

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

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

关于带有 bool 值的 Javascript If 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17351551/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com