gpt4 book ai didi

javascript - 如何通过单击按钮添加html输入

转载 作者:行者123 更新时间:2023-11-28 01:23:18 25 4
gpt4 key购买 nike

我正在尝试制作一个平均成绩计算器,现在一切顺利,但现在我想在每次单击按钮时添加一行输入,包括 html、javascript 以及分配的数字增加一个。输入应位于现有输入的下方,计算按钮应随之移动。我已经在这一点上停留了好几天了,感谢您的帮助!

function getValue(el){
return (+document.getElementById(el).value)
};

function calculator() {
var weight1=getValue('weight-1');
var mark1=getValue('mark-1');
var grade1=weight1*mark1;

var weight2=getValue('weight-2');
var mark2=getValue('mark-2');
var grade2=weight2*mark2;



var totalWeight=weight1+weight2;
var totalGrade=grade1+grade2;

var finalGrade=totalGrade/totalWeight;
var display=document.getElementById('outputDiv');


display.innerHTML='Je gemiddelde is: ' +finalGrade.toFixed(2);
}
body {
font-family: 'Roboto', sans-serif;
background-color: ;
}

h2, h3 {
text-align: center;
}

table {
margin: auto;
}

#table-title {
font-size: 20px;
font-style: italic;
text-align: center;
position: relative;
}

#weight-1, #weight-2 {
width: 100px;
}

input {
text-align: center;
}

#button-div {
position: relative;
width: 150px;
margin: auto;
}

#calc-button {
position: relative;
padding: 5px;
margin-top: 20px;
}
#calc-button:hover {
border-color: black;
box-shadow: 8px 8px 8px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
}

#outputDiv {
height: 50px;
text-align: center;
width: 300px;
margin: auto;
margin-top: 20px;
}
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
</head>
<header>
<h2>Gemiddelde cijfer</h2>
<h3>Voer hieronder je cijfers in</h3>
</header>

<body>
<table id="table">

<tr id="table-title">
<td>Weging</td>
<td>Cijfer</td>
</tr>

<tr>
<td><input id="weight-1" type="text" size=2 maxlength="5" value=""></td>
<td><input id="mark-1" type="text" size=2 maxlength="5" value=""></td>
</tr>

<tr>
<td><input id="weight-2" type="text" size=2 maxlength="5" value=""></td>
<td><input id="mark-2" type="text" size=2 maxlength="5" value=""></td>
</tr>

</table>
<div id="button-div">
<input id="calc-button" type="button" value="Berekenen je gemiddelde" onclick="calculator()">
</div>
<div id="outputDiv"></div>
</body>
</html>

最佳答案

您可以使用 querySelectoryAll 获取您的权重并标记输入,而不是对输入的 id 进行硬编码,这将为您提供所有输入,无论输入有多少,然后您可以循环遍历元素获取值,然后将 id 递增 1 的新输入行附加到 tbody。然后,下次您调用该函数时,将会有一个循环输入。这是一个基于您的代码的示例:

 
function calculator() {
var weight = 0;
var mark = 0;
var weights = document.querySelectorAll('[id^=weight-]');
var grades = document.querySelectorAll('[id^=mark-]');
var trs = document.getElementsByTagName('tr');
var tBody = document.getElementsByTagName('tbody')[0];
var totalWeight = 0;
var totalGrade = 0;

for (var i = 0; i < weights.length; i++) {
totalWeight += +weights[i].value;

}
for (var i = 0; i < grades.length; i++) {
totalGrade += +grades[i].value;
}


var finalGrade=totalGrade/totalWeight;
var display = document.getElementById('outputDiv');
var newTr = document.createElement('TR');
newTr.innerHTML = `<td><input id="weight-${trs.length + 1}" type="text" size=2 maxlength="5" value=""></td><td><input id="mark-${trs.length + 1}" type="text" size=2 maxlength="5" value=""></td>`
tBody.appendChild(newTr);
display.innerHTML='Je gemiddelde is: ' +finalGrade.toFixed(2);
}
body {
font-family: 'Roboto', sans-serif;
background-color: ;
}

h2, h3 {
text-align: center;
}

table {
margin: auto;
}

#table-title {
font-size: 20px;
font-style: italic;
text-align: center;
position: relative;
}

[id^="weight-"] {
width: 100px;
}

input {
text-align: center;
}

#button-div {
position: relative;
width: 150px;
margin: auto;
}

#calc-button {
position: relative;
padding: 5px;
margin-top: 20px;
}
#calc-button:hover {
border-color: black;
box-shadow: 8px 8px 8px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
}

#outputDiv {
height: 50px;
text-align: center;
width: 300px;
margin: auto;
margin-top: 20px;
}
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
</head>
<header>
<h2>Gemiddelde cijfer</h2>
<h3>Voer hieronder je cijfers in</h3>
</header>

<body>
<table id="table">

<tr id="table-title">
<td>Weging</td>
<td>Cijfer</td>
</tr>

<tr>
<td><input id="weight-1" type="text" size=2 maxlength="5" value=""></td>
<td><input id="mark-1" type="text" size=2 maxlength="5" value=""></td>
</tr>

<tr>
<td><input id="weight-2" type="text" size=2 maxlength="5" value=""></td>
<td><input id="mark-2" type="text" size=2 maxlength="5" value=""></td>
</tr>

</table>
<div id="button-div">
<input id="calc-button" type="button" value="Berekenen je gemiddelde" onclick="calculator()">
</div>
<div id="outputDiv"></div>
</body>
</html>

关于javascript - 如何通过单击按钮添加html输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51370580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com