javascript无法正常运行

时间:2017-08-16 20:14:57

标签: javascript html

为什么这个HTML / JavaScript代码无效?这是一个考试计算器。



function calculate() {
  var f = parseFloat(document.getElementById("first").value);
  var s = ParseFloat(document.getElementById("second").value);
  var q = parseFloat(document.getElementById("quizes").value);
  var h = parseFloat(document.getElementById("homeworks").value);
  var fi = parseFloat(document.getElementById("final").value);
  alert(f / 4 + s / 4 + q / 2 + h / 2 + fi * 0.4);
}

<p> first:<input type="number" id="first" </p>
<p> second:<input type="number" id="second" </p>
<p> quizes:<input type="number" id="quizes" </p>
<p> homeworks:<input type="number" id="homeworks" </p>
<p> final:<input type="number" id="final" </p>
<button onclick="calculate()">calculate</button>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

我不知道在实际代码中是复制错误还是错误,但是你在parseFloat上有一个大写字母P.遵循代码

function calculate() {
    var f = parseFloat(document.getElementById("first").value); 
    var s = parseFloat(document.getElementById("second").value); 
    var q = parseFloat(document.getElementById("quizes").value); 
    var h = parseFloat(document.getElementById("homeworks").value);
    var fi = parseFloat(document.getElementById("final").value); 

    alert(f / 4 + s / 4 + q / 2 + h / 2 + fi * 0.4);
} 

修改
你的HTML也有一些错误。 “输入”标签未关闭。

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>calculator</title>
</head>

<body>
    <p> first:<input type="number" id="first"></p>
    <p> second:<input type="number" id="second"> </p>
    <p> quizes:<input type="number" id="quizes"> </p>
    <p> homeworks:<input type="number" id="homeworks"> </p>
    <p> final:<input type="number" id="final"> 
    </p> <button onclick="calculate()">calculate</button>
    <script>
        function calculate() {
            var f = parseFloat(document.getElementById("first").value);
            var s = parseFloat(document.getElementById("second").value);
            var q = parseFloat(document.getElementById("quizes").value);
            var h = parseFloat(document.getElementById("homeworks").value);
            var fi = parseFloat(document.getElementById("final").value);

            alert(f / 4 + s / 4 + q / 2 + h / 2 + fi * 0.4);
        }
    </script>
</body>

</html>

答案 1 :(得分:0)

将解析而不是Parse

var s = ParseFloat (document.getElementById("second").value)
var s = parseFloat (document.getElementById("second").value)