javascript - 如何在转换后的数字旁边输出货币类型

标签 javascript html css

我试图让我的代码在转换后的数字旁边输出货币类型名称,例如 EURO、YEN 和 USD,但我似乎遇到了问题,如有任何帮助,我们将不胜感激。

function tipCalculator() {
  var billAmt = document.getElementById("bill").value;
  var tipValue = document.getElementById("tipValue").value;


  if (billAmt === "" || tipValue == 0) {
    alert("Please Enter Value")
    return;
  }


  var total = (billAmt * tipValue);
  total = Math.round(total * 100) / 100;
  total = total.toFixed(2);
  document.getElementById("totalTip").style.display = "block";
  document.getElementById("tip").innerHTML = total;

  if (document.getElementById("USD")) {

    document.getElementById("tip").innerHTML = total + " USD";
  }

}
document.getElementById("totalTip").style.display = "none";


document.getElementById("calculate").onclick =
  function() {
    tipCalculator();
  }
<select id="tipValue">
      <option value="">--Please choose a currency--</option>
      <option value ="1.30353" id="USD">USD</option>
      <option value ="1.16158" id="Euro">Euro</option>
      <option value ="8.75747" id="Yen">Yen</option>
      <option value ="4.98785" id="Zloty">Zloty</option>
</select>

最佳答案

您需要做的是根据您的货币选项(美元、欧元、日元、兹罗提等)创建一个数组,并在以下 中引用该数组而不是 "USD" >if 语句:

if(document.getElementById("USD")){
    document.getElementById("tip").innerHTML = total + " USD";
    }

像这样:

if(document.getElementById("currency_array[]")){
    document.getElementById("tip").innerHTML = total + " currency_array[]";
    }

关于javascript - 如何在转换后的数字旁边输出货币类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55554994/

相关文章:

javascript - React Hooks 状态未更新

javascript - 如何使用 highchart 导出带有自定义标记的仪表图表?

javascript - Jquery如何将对象传递给另一个函数

html table -- 设置column占用剩余空间,宽度至少300px

html - 如何超链接 CSS 制作的图像?

html - 允许 td 内容在没有 colspan 的情况下进入下一列

javascript - 卡片图片叠加,溢出卡片主体

javascript - 遍历特定子项 - jQuery/Javascript

javascript - jquery删除具有特定id的元素

html - CSS Grid Layout - grid-row-gap 每 2 行