javascript的计算器代码

时间:2012-10-23 02:31:50

标签: javascript javascript-events

我一直试图弄清楚我的代码有什么问题,我需要添加产品(香蕉,苏打水,薯片,糖果)然后再加上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>

1 个答案:

答案 0 :(得分: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>

如果你想让我解释一下,我会,但如果你真的没有学习的意图:不值得浪费我的时间。