如何使计算按钮工作

时间:2017-05-08 23:44:20

标签: javascript html calculator

我有这个我正在处理的代码,但无论我做什么,计算按钮都不起作用。我想使用pizzaSize和pizzaCost找到每平方英寸的成本。如何让计算按钮实际计算?

<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' >
<title> Pizza Calculator </title>
<script src="pizzaCalc.js">
</script>
</head>
<body>
<h1>Pizza Calculator</h1>
<p>
<label for="priceBox">Cost: </label><input type="text" id="priceBox" size="5"/></p>
<p>
    <label for="sizeBox">Diameter :</label><input type="text" id="sizeBox"     
    size="5"/>
</p>
<input type="button" id="cpsi" value="Cost PSI" onclick="calculate(cpsi)">

</body>
</html>

"use strict"
function pizzaCalc () {
    var size = document.getElementById ("sizeBox").value;
    size = parseFloat (size);
    var price = document.getElementById("priceBox").value;
    price = parceFloat (price);
    var costPerSquareInch = price / (3.14 * (size / 2)^2)
    alert('Pizza value :' +costPerSquareInch);
    document.getElementById("cpsi").value = costPerSquareInch;
}

2 个答案:

答案 0 :(得分:0)

<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' >
<title> Pizza Calculator </title>
<script src="pizzaCalc.js">
</script>
</head>
<body>
<h1>Pizza Calculator</h1>
<p>
<label for="priceBox">Cost: </label><input type="text" id="priceBox" value="5"/></p>
<p>
    <label for="sizeBox">Diameter :</label><input type="text" id="sizeBox" value="5"/>
</p>
<input type="button" id="cpsi" value="Cost PSI" onclick="pizzaCalc()">
    <script type="text/javascript">
    "use strict"
    function pizzaCalc () {
    var size = document.getElementById ("sizeBox").value;
    size = parseFloat (size);
    var price = document.getElementById("priceBox").value;
    price = parseFloat (price);
    var costPerSquareInch = price / (3.14 * (size / 2)^2)
    alert('Pizza value :' +costPerSquareInch);
    document.getElementById("cpsi").value = costPerSquareInch;
    }
    </script>
</body>
</html>

似乎

<input type="button" id="cpsi" value="Cost PSI" onclick="calculate(cpsi)">

应该是

<input type="button" id="cpsi" value="Cost PSI" onclick="pizzaCalc()">

我也假设输入标签上尺寸应为

答案 1 :(得分:-1)

<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' >
<title> Pizza Calculator </title>
<script src="pizzaCalc.js">
</script>
</head>
<body>
<h1>Pizza Calculator</h1>
<p>
<label for="priceBox">Cost: </label><input type="text" id="priceBox" value="5"/></p>
<p>
    <label for="sizeBox">Diameter :</label><input type="text" id="sizeBox" value="5"/>
</p>
<input type="button" id="cpsi" value="Cost PSI" onclick="pizzaCalc()">
    <script type="text/javascript">
    "use strict"
    function pizzaCalc () {
    var size = document.getElementById ("sizeBox").value;
    size = parseFloat (size);
    var price = document.getElementById("priceBox").value;
    price = parseFloat (price);
    var costPerSquareInch = price / (3.14 * (size / 2)^2)
    alert('Pizza value :' +costPerSquareInch);
    document.getElementById("cpsi").value = costPerSquareInch;