如何显示表格标题但值显示“?”
我有一个用于输入分数的表格,当用户单击“提交”按钮时,它会显示一个包含所有输入信息的表格。但是,我的问题是我想将表格标题显示为“平均分数”,但“平均分数”列的值应显示为“?” .让我介绍以下内容,我希望用纯 JS 完成这个练习,请帮忙。我真的很感激和欣赏。
这是我的 HTML 文件:
<body>
<script src="js/script.js"></script>
<h1 align="center">Class Marksheet</h1>
<table align="center">
<tr>
<td>Name:</td>
<td><input name="name" id="name" type="text" /></td>
</tr>
<tr>
<td>Math:</td>
<td>
<input name="math" min="0" max="10" id="math" type="number" />
</td>
</tr>
<tr>
<td>Physics:</td>
<td>
<input name="physics" min="0" max="10" id="physics" type="number" />
</td>
</tr>
<tr>
<td>Chemistry:</td>
<td>
<input name="chemical" min="0" max="10" id="chemical" type="number" />
</td>
</tr>
<td>
<button type="submit" onclick="score_table()">Submit</button>
</td>
</table>
<!--After the user enters the score and submits, this table will be displayed with the 2 buttons below
-->
<div id="divTable">
<table id="tableScore" border="5" align="center">
<th>No</th>
<th>Name</th>
<th>Math</th>
<th>Physics</th>
<th>Chemistry</th>
<th>Average Score</th> <!--Only show table heading but the value must be show "?"-->
</table>
<!--User press this button and the average score will be calculated and substituted for the value "?" in the "Average Score" column.-->
<button onclick="showAvg()" align="center">Calculate the average score</button>
<!--This is to demonstrate the average score >= 8-->
<button onclick="showBest()" align="center">
Best student
</button>
</div>
</body>
我的 JS 文件:
var testScore = {
name: "",
math: 0,
physical: 0,
chemistry: 0,
avg: 0
};
var i = 1;
// Show the table after "Submit"
function score_table() {
document.getElementById("divTable").style.display="block";
// Gathering the data
testScore.name = document.getElementById("name").value;
testScore.math = document.getElementById("math").value;
testScore.physical = document.getElementById("physics").value;
testScore.chemistry = document.getElementById("chemical").value;
// I know to calcualte the average score but i am struggle to show the value "?"
testScore.avg = ((parseFloat(testScore.math) + parseFloat(testScore.physical) + parseFloat(testScore.chemistry)) / 3).toFixed(2);
document.getElementById("name").value = "";
document.getElementById("math").value = "";
document.getElementById("physics").value = "";
document.getElementById("chemical").value = "";
// Add the data after submit
var table = document.getElementById("tableScore");
var row = table.insertRow(i);
var number = row.insertCell(0);
var name = row.insertCell(1);
var math = row.insertCell(2);
var physics = row.insertCell(3);
var chemistry = row.insertCell(4);
var avg = row.insertCell(5);
// Return the HTML value
number.innerHTML = i;
name.innerHTML = testScore.name;
math.innerHTML = testScore.math;
physics.innerHTML = testScore.physical;
chemistry.innerHTML = testScore.chemistry;
avg.innerHTML = testScore.avg;
i++;
}
// I want the calculated average score to replace the value "?" in the "Average Score" column
function showAvg() {
document.getElementById("tableScore").querySelector("th:nth-child(6)").style.display = "block";
var colAvg = document.getElementById("tableScore").querySelectorAll("td:nth-child(6n)");
for (var i = 0; i < colAvg.length; i++) {
colAvg[i].style.display = "block";
}
}
// If the average score >= 8 it will be red
function showBest() {
var colAvg = document.getElementById("tableScore").querySelectorAll("td:nth-child(6n)");
var rowAvg = document.getElementById("tableScore").querySelectorAll("tr:nth-child(1n)");
for (var i = 0; i < colAvg.length; i++) {
var avg = parseFloat(colAvg[i].innerText);
if (avg >= 8) {
rowAvg[i + 1].style.background = "red";
} else {}
}
}
我的 CSS 文件:
#divTable {
display: none;
text-align: center;
width: 100%;
}
#tableScore th:nth-child(6),
#tableScore td:nth-child(6) {
border-collapse: collapse;
border-spacing: 0;
width: 100%;
display: none;
}
I have a form to enter scores and when user clicks "Submit" button it shows a table which will contains all entered information. However, my problem is that I want to display the table heading as "average score" but the value of the "average score" column should be displayed "?" .Let me present the following and I want this exercise done in pure JS, please help. I am really grateful and appreciative.
Here is my HTML file:
<body>
<script src="js/script.js"></script>
<h1 align="center">Class Marksheet</h1>
<table align="center">
<tr>
<td>Name:</td>
<td><input name="name" id="name" type="text" /></td>
</tr>
<tr>
<td>Math:</td>
<td>
<input name="math" min="0" max="10" id="math" type="number" />
</td>
</tr>
<tr>
<td>Physics:</td>
<td>
<input name="physics" min="0" max="10" id="physics" type="number" />
</td>
</tr>
<tr>
<td>Chemistry:</td>
<td>
<input name="chemical" min="0" max="10" id="chemical" type="number" />
</td>
</tr>
<td>
<button type="submit" onclick="score_table()">Submit</button>
</td>
</table>
<!--After the user enters the score and submits, this table will be displayed with the 2 buttons below
-->
<div id="divTable">
<table id="tableScore" border="5" align="center">
<th>No</th>
<th>Name</th>
<th>Math</th>
<th>Physics</th>
<th>Chemistry</th>
<th>Average Score</th> <!--Only show table heading but the value must be show "?"-->
</table>
<!--User press this button and the average score will be calculated and substituted for the value "?" in the "Average Score" column.-->
<button onclick="showAvg()" align="center">Calculate the average score</button>
<!--This is to demonstrate the average score >= 8-->
<button onclick="showBest()" align="center">
Best student
</button>
</div>
</body>
My JS file:
var testScore = {
name: "",
math: 0,
physical: 0,
chemistry: 0,
avg: 0
};
var i = 1;
// Show the table after "Submit"
function score_table() {
document.getElementById("divTable").style.display="block";
// Gathering the data
testScore.name = document.getElementById("name").value;
testScore.math = document.getElementById("math").value;
testScore.physical = document.getElementById("physics").value;
testScore.chemistry = document.getElementById("chemical").value;
// I know to calcualte the average score but i am struggle to show the value "?"
testScore.avg = ((parseFloat(testScore.math) + parseFloat(testScore.physical) + parseFloat(testScore.chemistry)) / 3).toFixed(2);
document.getElementById("name").value = "";
document.getElementById("math").value = "";
document.getElementById("physics").value = "";
document.getElementById("chemical").value = "";
// Add the data after submit
var table = document.getElementById("tableScore");
var row = table.insertRow(i);
var number = row.insertCell(0);
var name = row.insertCell(1);
var math = row.insertCell(2);
var physics = row.insertCell(3);
var chemistry = row.insertCell(4);
var avg = row.insertCell(5);
// Return the HTML value
number.innerHTML = i;
name.innerHTML = testScore.name;
math.innerHTML = testScore.math;
physics.innerHTML = testScore.physical;
chemistry.innerHTML = testScore.chemistry;
avg.innerHTML = testScore.avg;
i++;
}
// I want the calculated average score to replace the value "?" in the "Average Score" column
function showAvg() {
document.getElementById("tableScore").querySelector("th:nth-child(6)").style.display = "block";
var colAvg = document.getElementById("tableScore").querySelectorAll("td:nth-child(6n)");
for (var i = 0; i < colAvg.length; i++) {
colAvg[i].style.display = "block";
}
}
// If the average score >= 8 it will be red
function showBest() {
var colAvg = document.getElementById("tableScore").querySelectorAll("td:nth-child(6n)");
var rowAvg = document.getElementById("tableScore").querySelectorAll("tr:nth-child(1n)");
for (var i = 0; i < colAvg.length; i++) {
var avg = parseFloat(colAvg[i].innerText);
if (avg >= 8) {
rowAvg[i + 1].style.background = "red";
} else {}
}
}
My CSS file:
#divTable {
display: none;
text-align: center;
width: 100%;
}
#tableScore th:nth-child(6),
#tableScore td:nth-child(6) {
border-collapse: collapse;
border-spacing: 0;
width: 100%;
display: none;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好了,享受吧
哦,还有更多你需要改变的 CSS 文件
There ya go, enjoy it
Oh, there more you need to change CSS file