简体   繁体   中英

Trouble with html input box

I'm making a basic usd converter for practice in html/javascript. However when I select the euro option it does the same as for the peso option.

<html>
    <head>
        <title>Currency Converter</title>
        <style>

        </style>
    </head>
    <body>
        <input id="amount"> </input>
        <p>usd Contverted to</p> 
        <p class="output"> </p>
        <select id="select"> <option value="1">Peso's</option> <option value="2">Euro's</option> </select>
        <p id="answer"> is </p>
        <input type="submit" value="Submit" onclick="run()"> 
        <script>
            function run() {
                var Amount = document.getElementById("amount").value;
                if (select = 1) {
                    document.getElementById("answer").innerHTML = "=-=-= " + Amount * 16.39  + "   =-=-=";
                } else if (select = 2) {
                    document.getElementById("answer").innerHTML = "=-=-= " + Amount * 0.9  + "   =-=-=";
                } else {
            }
        </script>
    </body>
</html>

You are not comparing select,you are setting it.

   if (select == 1) {
       document.getElementById("answer").innerHTML = ...
   } else if (select == 2) {
  • = -> setting a value
  • == -> comparing

它应该是select == 1 select=1将始终返回true,因为这样,您只是在分配一个值,而不检查是否相等

  • You are not getting value of select
  • You are not comparing select value in if condition, make it as select == 1 and select == 2

Complete solution here

Change your javaScript function as below

function run() {
  var Amount=document.getElementById("amount").value;
  var select=document.getElementById("select").value;
  if (select == 1) {
    document.getElementById("answer").innerHTML = "=-=-= " + Amount * 16.39  + "   =-=-=";
  } else if (select == 2) {
    document.getElementById("answer").innerHTML = "=-=-= " + Amount * 0.9  + "   =-=-=";
  } else {
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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