简体   繁体   English

通过获取 html(货币转换器)的 select 标记来乘以 javascript 中的不同值

[英]Multiply different value in javascript by getting select tags of html (currency converter)

I want to make a currency convert.我想做货币兑换。 mean using select tag of html select different currency and multiply with input value using javascript意思是使用 html select 不同货币的 select 标记并使用 ZDE9B9ED78D2E2E1DCE9FEE71 与输入值相乘

 function getInputValue(){ // Selecting the input element and get its value var inputVal = document.getElementById("myInput").value=currency; var dollar = inputVal*1.12; var answer = document.getElementById('answer'); answer.value= dollar; var inputVal = document.getElementById("myInput").value=currency; var pound = inputVal*0,86; var answer = document.getElementById('answer'); answer.value= pound; var inputVal = document.getElementById("myInput").value=currency; var pak = inputVal*170; var answer = document.getElementById('answer'); answer.value= pak; } function newVal(){ document.getElementById("myForm").reset(); }
 <select id="currency"> <option value="volvo" >Doller</option> <option value="volvo">Pound</option> <option value="volvo">Pak</option> </select>

A nice approach would be to use each option's value to hold the currency's exchange rate, like so:一个不错的方法是使用每个选项的value来保持货币的汇率,如下所示:

<select id="currency">
    <option value="1.12" >Dollar</option>
    <option value="0.86">Pound</option>
    <option value="170">Pak</option>
</select>

Then, in your Javascript function, you can simply multiply the amount by the selected option's value attribute:然后,在您的 Javascript function 中,您可以简单地将金额乘以所选选项的value属性:

function getInputValue(){
    //assuming you are typing the amount in an input with id "myInput"
    var amount = document.getElementById("myInput").value; 
    var rate = document.getElementById("currency").value;
    var result = amount * rate;

    //output the result to HTML
    //(assuming you have a div or element with id "result")
    document.getElementById("result").innerHTML = result;
}

On a side note, in your original code, I am not sure what you were trying to do with:附带说明一下,在您的原始代码中,我不确定您要做什么:

var inputVal = document.getElementById("myInput").value=currency;

Did you mean to real the value of currency ?你的意思是真正的currency价值? Then you should have done:那么你应该这样做:

var inputVal = document.getElementById("currency").value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM