繁体   English   中英

尝试从与另一种方法几乎相同的方法中获取 output 值,但该方法不起作用

[英]Trying to output value from a method that's almost identical to another method but this one is not working

我希望我的程序做什么
当我输入正确的折扣代码(“折扣”)时,将从总价中扣除 10%,并显示新的“总计”。 如果没有输入折扣代码/输入正确的折扣代码,则总计将与“总价”相同。

问题
总计没有显示任何内容。 这是我的 output,当我点击提交并输入正确的折扣代码时:[输出图像][1][1]:https://i.stack.imgur.com/WUwnK.png

我试过的
在我的 JS 脚本中,我尝试了以下操作:

<script>
function getGrandTotal(){
const total = document.getElementbyId("total").value;
const dis = document.getElementbyId("discountCode").value;

 if(dis === "discount"){
  document.getElementById("grandTotal").value=
 total * 0.9;
 }else{
grandTotal = total;
}
 }
document.getElementById('btnSubmit').addEventListener('click', getGrandTotal);
</script>

我的总金额分配在这个 function 中:

<script>
function getTotalAmount() {
const choice = document.getElementById("burgerSize").value;
const amount = document.getElementById("quantity").value;

if (choice === "4") {
  document.getElementById("total").value =
    amount * 4;
}
if (choice === "6") {
  document.getElementById("total").value =
    amount * 6;
}
if (choice === "10") {
  alert("Health Warning");
  document.getElementById("total").value =
    amount * 10;
}
}
document.getElementById('burgerSize').addEventListener('change', getTotalAmount);
document.getElementById('quantity').addEventListener('change', getTotalAmount);
</script>

我在想相同的方法结构将适用于这两项任务。 我还尝试使用 if(dis;== "discount"){grandTotal = total,} 代替我的 else 语句。 但这也行不通。 任何帮助都会很棒!

这是我的相关 html 代码:

<label> Total cost: </label>
<input type="text" id="total" /><br>

<label for = "discountCode"> Discount code: </label>
<input type="text" id="discountCode" /><br>
<input type="button" id="btnSubmit" value="Submit " onclick="return getGrandTotal()" /><br>

<label> Grand total: </label>
<input type="text" id="grandTotal" /><br>

只是一些语法错误。

<input type="button" id="btnSubmit" value="Submit " onclick="return getGrandTotal" /><br>

改成

onclick="getGrandTotal()"

document.getElementById区分大小写

工作代码在这里

暂无
暂无

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

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