[英]Converting currencies using JAVASCRIPT. can't set the value of converted input text field through my function
I am trying to make a currency converter using html and javascript (without jquery) . 我正在尝试使用html和javascript (不使用jquery)创建货币转换器。 I have tried a lot of things but I can't set the value of the text field to the converted amount.
我已经尝试了很多方法,但是无法将文本字段的值设置为转换后的金额。 What is going wrong with my code?
我的代码出了什么问题?
So what I am trying to do is basically check what currency my initial amount is, based on that call the right function to do the calculations and then set the value of the input field with id="cur2" to the result of those calculations. 因此,我要尝试的是基本上检查我的初始金额是哪种货币,基于该调用正确的函数进行计算,然后将 id =“ cur2”的输入字段的值设置为这些计算的结果。 I have tried to do that with the following javascript code .
我尝试使用以下javascript代码来做到这一点。 I am only giving you the function for euro to other currencies (the rest work similarly):
我仅向您提供欧元对其他货币的功能(其余工作类似):
window.onload = function() { document.getElementById("btnConvert").addEventListener("click", convert); } function convert() { var select = document.getElementById("currency1"); var curr = select.options[select.selectedIndex].value; var val = document.getElementById("cur2").value; if (curr == "EUR") { val = EURtoOther(); } if (curr == "USD") { val = USDtoOther(); } if (curr == "GBP") { val = GBPtoOther(); } if (curr == "AUD") { val = AUDtoOther(); } if (curr == "JPY") { val = JPYtoOther(); } } function EURtoOther() { var select = document.getElementById("currency2"); var curr = select.options[select.selectedIndex].value; var initval = document.getElementById("cur1").value; if (curr == "EUR") { return initval * 1.0; } if (curr == "USD") { return initval * 1.13; } if (curr == "GBP") { return initval * 0.79; } if (curr == "AUD") { return initval * 1.55; } if (curr == "JPY") { return initval * 123.27; } }
<select id="currency1"> <option value="EUR">Euro</option> <option value="USD">US Dollar</option> <option value="GBP">British Pound</option> <option value="AUD">Australian Dollar</option> <option value="JPY">Japanese Yen</option> </select> <input type="number" id="cur1"> <button id="btnConvert">Convert</button> <select id="currency2"> <option value="EUR">Euro</option> <option value="USD">US Dollar</option> <option value="GBP">British Pound</option> <option value="AUD">Australian Dollar</option> <option value="JPY">Japanese Yen</option> </select> <input type="text" id="cur2">
Not working and I don't know why. 不工作,我不知道为什么。 Thanks in advance.
提前致谢。
You have to do document.getElementById().value = val;
您必须要做
document.getElementById().value = val;
. 。
val
is not reffering to that element, it is just a value so you need to set it after. val
与该元素无关,它只是一个值,因此需要在之后进行设置。
https://jsfiddle.net/stevenkaspar/aphxcpcs/ https://jsfiddle.net/stevenkaspar/aphxcpcs/
You are not setting the value of second textbox. 您没有设置第二个文本框的值。 Use
采用
document.getElementById("cur2").value = val;
to set the value of second textbox 设置第二个文本框的值
window.onload = function() { document.getElementById("btnConvert").addEventListener("click", convert); } function convert() { var select = document.getElementById("currency1"); var curr = select.options[select.selectedIndex].value; var val = document.getElementById("cur2").value; if (curr == "EUR") { val = EURtoOther(); } if (curr == "USD") { // val = USDtoOther(); } if (curr == "GBP") { // val = GBPtoOther(); } if (curr == "AUD") { // val = AUDtoOther(); } if (curr == "JPY") { // val = JPYtoOther(); } document.getElementById("cur2").value = val; } function EURtoOther() { var select = document.getElementById("currency2"); var curr = select.options[select.selectedIndex].value; var initval = document.getElementById("cur1").value; if (curr == "EUR") { return initval * 1.0; } if (curr == "USD") { return initval * 1.13; } if (curr == "GBP") { return initval * 0.79; } if (curr == "AUD") { return initval * 1.55; } if (curr == "JPY") { return initval * 123.27; } }
<select id="currency1"> <option value="EUR">Euro</option> <option value="USD">US Dollar</option> <option value="GBP">British Pound</option> <option value="AUD">Australian Dollar</option> <option value="JPY">Japanese Yen</option> </select> <input type="number" id="cur1"> <button id="btnConvert">Convert</button> <select id="currency2"> <option value="EUR">Euro</option> <option value="USD">US Dollar</option> <option value="GBP">British Pound</option> <option value="AUD">Australian Dollar</option> <option value="JPY">Japanese Yen</option> </select> <input type="text" id="cur2">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.