【发布时间】:2018-10-13 15:20:57
【问题描述】:
我正在尝试创建一个网站来计算某人在某个学科中的年平均成绩。但是,当单击“计算”按钮时,它会返回“[object HTMLHeadingElement][object HTMLHeadingElement][object HTMLHeadingElement][object HTMLHeadingElement][object HTMLHeadingElement]NaN”。怎么了?
<!DOCTYPE html>
<html lang="pt-br">
<meta charset="utf-8"/>
<head>
<title>Annual Average Grade</title>
<meta name="description" content="Calculate your annual average grade."/>
<link rel="apple-touch-icon" sizes="180x180" href="../favicons/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="../favicons/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="192x192" href="../favicons/android-chrome-192x192.png">
<link rel="icon" type="image/png" sizes="16x16" href="../favicons/favicon-16x16.png">
<link rel="manifest" href="../favicons/site.webmanifest">
<link rel="mask-icon" href="../favicons/safari-pinned-tab.svg" color="#5bbad5">
<link rel="shortcut icon" href="../favicons/favicon.ico">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="msapplication-TileImage" content="../favicons/mstile-144x144.png">
<meta name="msapplication-config" content="../favicons/browserconfig.xml">
<meta name="theme-color" content="#ffffff">
</head>
<style>
h1, h2, h3, h4, h5, h6 {
font-family: 'Segoe UI';
}
a, b {
font-family: 'Segoe UI';
}
button {
border-radius: 5px 5px 5px 5px;
background-color: white;
}
input {
border-radius: 5px 5px 5px 5px;
border-color: black;
border-width: 1px;
height: 25px;
}
.align-left {
text-align: left;
}
.align-center {
text-align: center;
}
.align-right {
text-align: right;
}
.title {
text-align: center;
font-weight: bolder;
}
.description {
text-align: center;
font-size: 17px;
font-weight: lighter;
}
.subtitle {
text-align: center;
font-size: 30px;
font-weight: bolder;
}
.grade {
text-align: center;
margin-left: 15px;
}
</style>
<body>
<h1 class="title" style="margin-left: 15px">Annual Average Grade</h1>
<h2 class="description" style="margin-top: -20px">Calculate your annual average grade.</h2>
<br/>
<br/>
<h3 class="subtitle">Mathematics</h3>
<div class="grade">
<h4 id="first-grade">1st grade:</h3><input type="number"/>
<h4 id="second-grade">2nd grade:</h3><input type="number"/>
<h4 id="third-grade">3rd grade:</h3><input type="number"/>
<h4 id="fourth-grade">4th grade:</h3><input type="number"/>
<h4 id="fifth-grade">5th grade</h3><input type="number"/>
<h4 id="sixth-grade">6th grade:</h3><input type="number"/>
</div>
<br/>
<div class="align-center">
<button onclick="calcGradeAverage()">Calculate</button>
</div>
<script>
function calcGradeAverage() {
var firstGrade = document.getElementById('first-grade');
var secondGrade = document.getElementById('second-grade');
var thirdGrade = document.getElementById('third-grade');
var fourthGrade = document.getElementById('fourth-grade');
var fifthGrade = document.getElementById('fifth-grade');
var sixthGrade = document.getElementById('sixth-grade');
var res = firstGrade + secondGrade + thirdGrade + fourthGrade + fifthGrade + sixthGrade / 6;
document.write(res);
}
</script>
</body>
</html>
【问题讨论】:
-
您检索的是标题,而不是输入。
-
@j08691 如何检索输入?
-
最简单的方法是将 ID 从标题移动到输入。然后你可以做
parseInt( document.getElementById('first-grade').value, 10); ...,这将返回正在输入的字符串的整数值。
标签: javascript math average