这是我的代码。不知道为什么它不工作需要帮助检查我的代码。 我正在尝试根据用户输入的值计算平均值和总和。 HTML运行良好,但我的JavaScript代码有点错误。请帮忙
< script >
function calc() {
int a = parseInt(document.getElementsByName("english").value);
int b = parseInt(document.getElementsByName("bmelayu").value);
int c = parseInt(document.getElementsByName("math").value);
int d = parseInt(document.getElementsByName("geo").value);
int e = parseInt(document.getElementsByName("hist").value);
int sum = a + b + c + d + e;
int average = (a + b + c + d + e) / 5;
document.getElementById("sum").innerHTML = sum;
document.getElementById("average").innerHTML = average;
} < /script>
<html>
<head>
<title>Assignment3</title>
</head>
<script>
function calc() {
int a = parseInt(document.getElementsByName("english").value);
int b = parseInt(document.getElementsByName("bmelayu").value);
int c = parseInt(document.getElementsByName("math").value);
int d = parseInt(document.getElementsByName("geo").value);
int e = parseInt(document.getElementsByName("hist").value);
int sum = a + b + c + d + e;
int average = (a + b + c + d + e) / 5;
document.getElementById("sum").innerHTML = sum;
document.getElementById("average").innerHTML = average;
}
</script>
<body>
<form name="form1">
English :
<input type="number" name="english">
<br>Bahasa Melayu :
<input type="number" name="bmelayu">
<br>Math :
<input type="number" name="math">
<br>Geography :
<input type="number" name="geo">
<br>History :
<input type="number" name="hist">
<br>
<button onclick="calc()">Calculate</button>
</form>
<p id="sum"></p>
<p id="average"></p>
</body>
</html>
答案 0 :(得分:0)
function calc() {
var a = parseInt(document.getElementsByName("english")[0].value);
var b = parseInt(document.getElementsByName("bmelayu")[0].value);
var c = parseInt(document.getElementsByName("math")[0].value);
var d = parseInt(document.getElementsByName("geo")[0].value);
var e = parseInt(document.getElementsByName("hist")[0].value);
var sum = a + b + c + d + e;
var average = (a + b + c + d + e) / 5;
document.getElementById("sum").innerHTML = sum;
document.getElementById("average").innerHTML = average;
}
答案 1 :(得分:0)
function calc() {
var a = parseInt(document.getElementById("english").value);
var b = parseInt(document.getElementById("bmelayu").value);
var c = parseInt(document.getElementById("math").value);
var d = parseInt(document.getElementById("geo").value);
var e = parseInt(document.getElementById("hist").value);
console.log(a);
var sum = a + b + c + d + e;
var average = (a + b + c + d + e) / 5;
document.getElementById("sum").innerHTML = sum;
document.getElementById("average").innerHTML = average;
}
<html>
<head>
<title>Assignment3</title>
</head>
<body>
English :
<input type="number" id="english">
<br>Bahasa Melayu :
<input type="number" id="bmelayu">
<br>Math :
<input type="number" id="math">
<br>Geography :
<input type="number" id="geo">
<br>History :
<input type="number" id="hist">
<br>
<button onclick="calc();">Calculate</button>
<p id="sum"></p>
<p id="average"></p>
</body>
</html>
答案 2 :(得分:0)
嘿,我很高兴你找到了答案,但我认为如果你看到不同的方法,它可能对你有所帮助:
function calc() {
var elements = Array.prototype.slice.call(document.querySelectorAll(".grade-val"));
var grades = elements.map(function(element) {
return element.value;
});
var sum = grades.reduce(function(s, c) {
return parseInt(s, 10) + parseInt(c, 10);
});
var average = sum / grades.length;
document.getElementById("sum").innerHTML = sum;
document.getElementById("average").innerHTML = average;
}
<html>
<head>
<title>Assignment3</title>
</head>
<body>
English :
<input type="number" class="grade-val" name="english">
<br>Bahasa Melayu :
<input type="number" class="grade-val" name="bmelayu">
<br>Math :
<input type="number" class="grade-val" name="math">
<br>Geography :
<input type="number" class="grade-val" name="geo">
<br>History :
<input type="number" class="grade-val" name="hist">
<br>
<button onclick="calc();">Calculate</button>
<p id="sum"></p>
<p id="average"></p>
</body>
</html>
请注意,我在每个元素上添加了一个类grade-val
,如果您只是想添加另一个要考虑的主题,则不必修改代码,只需要修改标记(插入)具有相同类的新元素)。就这么简单。
这适用于IE8+
答案 3 :(得分:0)
我已经修复了你的代码片段 - 你在那里犯了很多错误。现在它可以工作
function calc() {
var a = parseInt(document.getElementsByName("english")[0].value);
var b = parseInt(document.getElementsByName("bmelayu")[0].value);
var c = parseInt(document.getElementsByName("math")[0].value);
var d = parseInt(document.getElementsByName("geo")[0].value);
var e = parseInt(document.getElementsByName("hist")[0].value);
var sum = a + b + c + d + e;
var average = (a + b + c + d + e) / 5;
document.getElementById("sum").innerHTML = sum;
document.getElementById("average").innerHTML = average;
}
&#13;
<html>
<head>
<title>Assignment3</title>
</head>
<script>
function calc() {
int a = parseInt(document.getElementsByName("english")[0].value);
int b = parseInt(document.getElementsByName("bmelayu")[0].value);
int c = parseInt(document.getElementsByName("math")[0].value);
int d = parseInt(document.getElementsByName("geo")[0].value);
int e = parseInt(document.getElementsByName("hist")[0].value);
int sum = a + b + c + d + e;
int average = (a + b + c + d + e) / 5;
document.getElementById("sum").innerHTML = sum;
document.getElementById("average").innerHTML = average;
}
</script>
<body>
<form name="form1">
English :
<input type="number" name="english">
<br>Bahasa Melayu :
<input type="number" name="bmelayu">
<br>Math :
<input type="number" name="math">
<br>Geography :
<input type="number" name="geo">
<br>History :
<input type="number" name="hist">
<br>
<button onclick="calc()">Calculate</button>
</form>
<p id="sum"></p>
<p id="average"></p>
</body>
</html>
&#13;