简体   繁体   English

使用JAVASCRIPT转换货币。 无法通过我的函数设置转换后的输入文本字段的值

[英]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.

相关问题 Javascript将输入文本字段设置为值不起作用 - Javascript set input text field to value not working 使用javascript无法获取输入字段值? - Can't get input field value using javascript? mySQL和javascript。 似乎无法使用javascript更新我的数据库 - mySQL & javascript. Can't seem to UPDATE my database using javascript 为什么我不能在其中输入文字<input>带有 javascript 代码的字段? 文字闪回原值 - Why can't I input text into this <input> field with javascript code? The text flashes back to original value 对 Javascript 非常陌生。 似乎无法让 1 个函数在我的代码中工作(berekenBedrag)。 需要计算总数 - Very new to Javascript. Can't seem to get 1 function to work in my code (berekenBedrag). Needs to calculate the total 无法获取 JavaScript 中输入字段的值 - Can’t get value of input field in JavaScript 如何在文本字段输入上执行javascript函数并用新值更新字段 - How can I execute a javascript function on text field input and update the field with new value 如何通过 JavaScript 设置输入隐藏字段的值? - How to set the value of a input hidden field through JavaScript? javascript。 将变量传递到变量输入字段 - javascript. passing a variable to a variable input field 可以解释这段javascript。 有人通过Facebook将它发送给我,并要求我将其复制到我的地址栏中 - 我没有 - Can interpret this piece of javascript. Someone sent it to me through facebook and asked that I copy it into my address box — I haven't
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM