我正在尝试使具有某个类名的所有文本字段的值相互更新,我的意思是,如果您有 2 个带有 class="a" 的文本字段,并且我在一个文本字段中输入一些内容,另一个将更新我输入的内容。这是我的代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type='text/javascript'>
function run()
{
var eq = document.getElementById("Energyq");
var m = document.getElementsByClassName("mass");
var cHeat = document.getElementById("cHeat");
var dT = document.getElementById("deltaTemprature");
var ek = document.getElementById("Energyk");
var v = document.getElementById("velocity");
var gc = document.getElementById("gravityCoefficient")
function classUpdate(class, changedId)
{
var x = document.getElementsByClassName(class);
var temp = x[changedId].value;
for(var i = 0; i < x.length; i++) {
x[i].value = temp.value;
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome</title>
</head>
<body>
<form action="">
<table style="font-weight: bold; font-size: 35px" border="10">
<tr style="text-align: center">
<td>Eq</td><td> = </td><td>m<td> * </td></td><td>c<td> * </td></td><td>ΔT</td>
</tr>
<tr style="text-align: center">
<td><input type="number" type="number" style="max-width: 105px" id="Energyq" class="energy" /></td><td> = </td><td><input type="number" style="max-width: 105px" max="10" class="mass" id="mass1" onkeyup="classUpdate("mass",1)" onchange="classUpdate("mass",1)" /></td><td> * </td><td><input type="number" style="max-width: 105px" id="cHeat" /></td><td> * </td><td><input type="number" style="max-width: 105px" id="deltaTemprature" /></td>
</tr>
<tr>
<td style="text-align: center">Ek</td><td style="text-align: center"> = </td><td style="text-align: left">(m</td><td style="text-align: center">*</td><td style="text-align: right">v² )</td><td style="text-align: center"> /</td><td style="text-align: center"> 2</td>
</tr>
<tr style="text-align: center">
<td><input type="number" style="max-width: 105px" id="Energyk" class="energy" /></td><td> = </td><td>(<input type="number" style="max-width: 105px" max="10" class="mass" id="mass2" h="10" onkeyup="classUpdate("mass",2)" onchange="classUpdate("mass",2)" /></td><td> * </td><td><input type="number" style="max-width: 105px" id="velocity" /><font size="6.5">²</font> )</td><td> /</td><td> 2</td>
</tr>
<tr style="text-align: center">
<td>Eh</td><td> = </td><td>m</td><td> * </td><td>g</td><td> * </td><td>h</td>
</tr>
<tr style="text-align: center">
<td><input type="number" style="max-width: 105px" id="Energyh" class="energy" /></td><td> = </td><td><input type="number" style="max-width: 105px" max="10" class="mass" id="mass3" onkeyup="classUpdate("mass",3)" onchange="classUpdate("mass",3)" /></td><td> * </td><td><input type="number" style="max-width: 105px" id="gravitatinalCoefficient" /></td><td> * </td><td><input type="number" style="max-width: 105px" id="height" /></td>
</tr>
<br />
</table>
<label><input type="checkbox" id="massConservation" /> Conservation of mass</label>
<br />
<label><input type="checkbox" id="energyConservation" /> Conservation of energy</label>
<br />
<br />
<input type="reset" /> <input type="button" onclick="run()" value="Calculate" />
<h6>by X</h6>
</form>
</body>
</html>
我究竟做错了什么?
谢谢。