-4

我一直试图弄清楚我的代码有什么问题,我需要添加产品(香蕉、苏打水、薯片、糖果),然后将其乘以 10% 的税收。我错过了这些产品的变量吗?我知道有些东西不见了,但我不知道该怎么办!

<html>
  <head>
    <title>Total Calculator</title>
  </head>
  <body>
    <p>
      Bananas: <input type="text" id="bananasBox" value="" /> at $ 0.50 a piece<br/>
      Sodas: <input type="text" id="sodasBox" value="" /> at $ 0.75 per can<br/>
      Chips: <input type="text" id="chipsBox" value="" /> at $1.25 per bag<br/>
      Candy: <input type="text" id="candyBox" value="" /> at $1.00 per pack<br/>
      TAX is 10 %
    </p>

    <button id="Calculate" onclick= "Calculate()" value="Calculate">Calculate</button>

    <script>
      function Calculate(){
        var total = 0;
        var cost = document.getElementById("cost").value;
        var tax = document.getElementById("tax").value;

        total = cost * tax;
        document.getElementById("total").value = total;

        document.getElementById("outputDiv").innerHTML= "Your TOTAL is: " + total;
      }
    </script>
    <hr/>
    <div id="outputDiv"> 
    </div>
  </body>
</html>
4

1 回答 1

0

我不认为你真的付出了任何努力,但这是一个正确的答案:

<html>
<head>
<title> Total Calculator </title>
</head>
<body>
<p>
Bananas:   <input type="text" id="bananasBox" value=""> at $ 0.50 a piece<br>
Sodas  :   <input type="text" id="sodasBox"   value=""> at $ 0.75 per can<br>
Chips  :   <input type="text" id="chipsBox"   value=""> at $1.25 per bag<br>
Candy  :   <input type="text" id="candyBox"   value=""> at $1.00 per pack<br>
TAX is 10 %
</p>
<input type="button" value="Calculate" id='Calculate' onclick= "Calculate()">
<script type="text/javascript">


function Calculate() {
    var total = 0;
    var bananas = Number(document.getElementById("bananasBox").value) * .5;
    var sodas = Number(document.getElementById("sodasBox").value) * .75;
    var chips = Number(document.getElementById("chipsBox").value) * 1.25;
    var candy = Number(document.getElementById("candyBox").value) * 1;
    var tax = 0.10;

    total = (bananas+sodas+chips+candy)*(1+tax);
document.getElementById('outputDiv').innerHTML= 'Your TOTAL is: $' + Number(total).toFixed(2);
}
</script>
<hr>
<div id="outputDiv"> 
</div>
</body>
</html>

如果您要我解释,我会的,但是如果您真的没有学习的意图:那不值得浪费我的时间。

于 2012-10-23T02:41:49.893 回答