我试图让我的代码输出货币类型的名称,例如欧元,日元和美元旁边的转换数字,但我似乎有麻烦,任何帮助将不胜感激。
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>
发布于 2019-04-07 10:26:04
您需要做的是从您的货币选项(美元、欧元、日元、兹罗提等)创建一个数组。并在下面的if语句中引用该数组,而不是"USD":
if(document.getElementById("USD")){
document.getElementById("tip").innerHTML = total + " USD";
}如下所示:
if(document.getElementById("currency_array[]")){
document.getElementById("tip").innerHTML = total + " currency_array[]";
}发布于 2019-04-07 10:43:31
@elbrant仍然有同样的问题,因为你需要知道你在数组中的位置。您需要传递id以使其成为动态的。所以,我会让你的选项进入点击事件,这样它就可以传递它的id:
document.getElementById("option").onclick =
function() {
tipCalculator(this.id);
}不要忘记将参数添加到函数中。
在函数中,去掉if语句并执行以下操作:
document.getElementById("tip").innerHTML = total + this.id;https://stackoverflow.com/questions/55554994
复制相似问题