Javascript JS-根据选定的下拉列表计算值
所以我有一个货币计算器,它显示了你输入的金额,但不同的货币价值。(例如美元兑欧元) 以下是HTML:Javascript JS-根据选定的下拉列表计算值,javascript,html,Javascript,Html,所以我有一个货币计算器,它显示了你输入的金额,但不同的货币价值。(例如美元兑欧元) 以下是HTML: <b> Exchange money </b> <br> <br> Enter amount for RSD: <input type="number" name="nbsAmount" id="nbsAmount" size="5"> <br> <button class="dugme">Calcula
<b> Exchange money </b> <br> <br>
Enter amount for RSD: <input type="number" name="nbsAmount" id="nbsAmount" size="5"> <br>
<button class="dugme">Calculate</button> <br> <br>
Evro value is: <div class="konacnaEvroVrednost"></div>
Dolar value is: <div class="konacnaDolarVrednost"></div>
Swiss value is: <div class="konacnaSwissrednost"></div>
这个很好用。如你所见:
这是小提琴:
但是现在我想改变它,让它更动态地工作。
我希望有两个下拉列表,让您选择要从更改为的值。这样地:
到目前为止,我得到的是:
问题在于,一个输入值应复制到另一个输入值,但应使用货币的附加值
因此,如果我选择RSD并设置1200,其他美元,那么其他输入应显示11.4
所以我在这里有点纠结于如何实现这一点。首先,您已将
myFunction
绑定到按钮onClick事件,但您尚未使用此名称定义函数。单击按钮后,您可以在控制台中看到以下错误
Uncaught ReferenceError: myFunction is not defined
您必须定义此函数:
window.myFunction = function() {...}
或者更好,将事件侦听器添加到按钮单击:
document.getElementById('buttonId').addEventListener('click', function() {...})
为了计算动态汇率,我首先将输入金额转换为单一货币(例如RSD),然后将该值乘以正确的汇率
我修改了JSFIDLE()以反映这些更改。您可以创建一个字典,其中包含下拉列表的“id”和转换率,也可以为两个下拉列表提供相同的id。然后你只需要把这个值乘以速率,然后把结果加到第二个输入中
var rsd = 1;
var evro = 0.0085;
var dolar = 0.0095;
var frank = 0.0096;
var dict = {
"4":rsd,
"1":evro,
"3":dolar,
"2":frank
}
function myFunction(){
var mvs = document.getElementById('mojaVrednostSelect').value;
var nvs = document.getElementById('novaVrednostSelect').value;
var mv = document.getElementById('mojaVrednost').value;
var nv = document.getElementById('novaVrednost').value;
novaVrednost.value = parseInt(mojaVrednost.value) * dict[nvs]
console.log("Yoooo"+ dict[nvs])
console.log("mvs je" + mvs);
console.log("nvs je" + nvs);
console.log("======");
console.log("mojaVrednost je" + mojaVrednost.value);
console.log("novaVrednost je" + novaVrednost.value);
}
document.getElementById('button').onclick = myFunction
希望这对你有帮助
var rsd = 1;
var evro = 0.0085;
var dolar = 0.0095;
var frank = 0.0096;
var dict = {
"4":rsd,
"1":evro,
"3":dolar,
"2":frank
}
function myFunction(){
var mvs = document.getElementById('mojaVrednostSelect').value;
var nvs = document.getElementById('novaVrednostSelect').value;
var mv = document.getElementById('mojaVrednost').value;
var nv = document.getElementById('novaVrednost').value;
novaVrednost.value = parseInt(mojaVrednost.value) * dict[nvs]
console.log("Yoooo"+ dict[nvs])
console.log("mvs je" + mvs);
console.log("nvs je" + nvs);
console.log("======");
console.log("mojaVrednost je" + mojaVrednost.value);
console.log("novaVrednost je" + novaVrednost.value);
}
document.getElementById('button').onclick = myFunction