javascript - 从特定行访问表格单元格以检索其嵌入的输入值
问题描述
我希望该函数在下表中的值上返回此操作的结果:
a+b*c-(e/f)
这个动作在没有 e 和 f 的行的情况下起作用。我在这里发现了类似的问题,但是对于这一特殊的最后一行没有解决方案。我很确定我应该如何从 e 和 f 输入中获得价值。函数 shul 对每一行都是 workinf——不管它们有多少。
function calc(id) {
var row = id.parentNode.parentNode;
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;
var e = row.cells[0].getElementsByTagName('input')[1].value; //not working
var f = row.cells[1].getElementsByTagName('input')[1].value; //not working
var res = +a + +b * c - (e / f);
row.cells[3].getElementsByTagName('input')[0].value = res;
}
body {
font-family: "Calibri";
margin: 0;
}
#inTotal {
font-weight: bold;
border-bottom: 2px solid black;
}
.nr {
font-weight: bold;
}
input {
border: none;
background: transparent;
text-align: center;
}
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
th {
background-color: #b9c1ce;
}
.Left {
text-align: left;
margin-left: 5px;
}
<table id="myTable">
<tr>
<th align="center" width="5%">A</th>
<th align="center" width="10%">B</th>
<th align="center" width="5%">C</th>
<th align="center" width="15%">Result</th>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" name="total" min="0" max="99999999"></td>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" name="total" min="0" max="99999999"></td>
</tr>
<tr>
<th align="center" width="5%">E</th>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<th align="center" width="5%">F</th>
<td align="center" width="15%"><input align="center" type="text" name="total" min="0" max="99999999"></td>
</tr>
</table>
更新我写了一个函数,该函数应该计算来自带有变量 e 和 f 的行的数据的值。但不幸的是,它缺乏对所有行执行?我不确定如何设置要服务的行数?
function calc(element) {
var row = element.parentNode.parentNode;
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;
// Getting the last row
// var lastRow = document.getElementById("last-values");
// Then, retrieving e and f
//var e = lastRow.cells[1].getElementsByTagName('input')[0].value;
//var f = lastRow.cells[3].getElementsByTagName('input')[0].value;
var res = +a + +b * c;
row.cells[3].getElementsByTagName('input')[0].value = res;
}
function lastEle()
{
var y = document.getElementById('e').value;
var z = document.getElementById('f').value;
var mytable = document.getElementById('myTable');
var myinputs = mytable.getElementsByTagName('input');
var rowy = mytable.rows.length;
for (var i = 1; i < mytable.rows.length; i++) {
var tmp = mytable.rows[i].myinputs[3].value;
var wynik = +y + +z + +tmp;
mytable.rows[i].myinputs[4].value = wynik;
}
}
body {
font-family: "Calibri"; margin:0;}
#inTotal {
font-weight: bold;
border-bottom: 2px solid black;
}
.nr {
font-weight: bold;
}
input {
border: none;
background: transparent;
text-align: center;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th{
background-color: #b9c1ce;
}
.Left {
text-align: left;
margin-left: 5px;
}
<table id="myTable">
<tr class="cipa">
<th align="center" width="5%">A</th>
<th align="center" width="10%">B</th>
<th align="center" width="5%">C</th>
<th align="center" width="15%">D</th>
<th align="center" width="15%">F</th>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" onchange="calc(this);lastEle();" id="ads" name="total" min="0" max="99999999"></td>
<td align="center" width="15%"><input align="center" type="text" onkeyup="lastEle();" value="3" name="total" min="0" max="99999999"></td>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" onchange="calc(this);lastEle();" name="total" min="0" max="99999999"></td>
<td align="center" width="15%"><input align="center" onkeyup="lastEle();" type="text" name="total" min="0" max="99999999"></td>
</tr>
<tr>
<!-- e -->
<th>e</th>
<td><input type="text" onkeyup="lastEle();" id="e" value="" /></td>
<!-- f -->
<th>f</th>
<td><input type="text" id="f" onkeyup="lastEle();" value="" /></td>
</tr>
</table>
解决方案
你这样做了:
<table>
<tr>
<td><input type="text" /></td>
<td><input type="text" /></td>
<td><input type="text" /></td>
<td><input type="text" /></td>
</tr>
</table>
假设row
指向<tr>
,您可以通过这种方式访问它们,x
单元(n+1)th
格和输入数据y
在哪里:(n+1)th
row.cells[x].getElementsByTagName('input')[y];
在这里,y
只能等于0
,因为元素中只嵌入了一个输入td
元素。要访问“e 和 f”行,只需为其行设置一个 id:
<tr id="last-values">
<!-- e -->
<th>e</th>
<td><input type="text" value="5" /></td>
<!-- f -->
<th>f</th>
<td><input type="text" value="6" /></td>
</tr>
然后,重写你的 calc() 函数:
function calc(element) {
var row = element.parentNode.parentNode;
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;
// Getting the last row
var lastRow = document.getElementById("last-values");
// Then, retrieving e and f
var e = lastRow.cells[1].getElementsByTagName('input')[0].value;
var f = lastRow.cells[3].getElementsByTagName('input')[0].value;
var res = +a + +b * c - (e / f);
row.cells[3].getElementsByTagName('input')[0].value = res;
}
请记住,如果 e 或 f 被更新,您应该编写另一个函数:在这种情况下,您必须循环整个行集以更新值。我让你写这部分。
在工作片段下方(没有更新 e 和 f 的部分)。
function calc(element) {
var row = element.parentNode.parentNode;
var lastRow = document.getElementById("last-values");
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;
var e = lastRow.cells[1].getElementsByTagName('input')[0].value;
var f = lastRow.cells[3].getElementsByTagName('input')[0].value;
var res = +a + +b * c - (e / f);
row.cells[3].getElementsByTagName('input')[0].value = res;
}
table {
width: 100%;
border-collapse: collapse;
table-layout: fixed;
}
table th,
table td {
border: 1px solid black;
}
table th {
background: #a0a0a0;
}
table td input {
display: block;
width: 100%;
}
<table>
<tr>
<th>a</th>
<th>b</th>
<th>c</th>
<th>res</th>
</tr>
<tr>
<!-- a -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- b -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- c -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- res -->
<td><input type="text" onKeyUp="calc(this)" /></td>
</tr>
<tr id="last-values">
<!-- e -->
<th>e</th>
<td><input type="text" value="5" /></td>
<!-- f -->
<th>f</th>
<td><input type="text" value="6" /></td>
</tr>
</table>
推荐阅读
- ruby-on-rails - 在rails上添加最喜欢的按钮ruby
- amazon-web-services - 当我对等不同区域的两个 VPC 时,流量是否会通过公共 Internet?
- python - 如何将视频(例如缩放通话)裁剪成多个文件?
- javascript - .forEach(...) 在获取时未定义
- html - 使用输入删除 mat-form-field 的边框(也用于悬停和焦点)
- javascript - 带有条件输出/应用的 Javascript 数组过滤器
- java - 在 java 程序中使用来自 hub.KerasLayer 和 python 和 tensorflow 2.1.0 的模型时,“图中没有名为 [input] 的操作”
- dataframe - 如何识别列pysapk中的第一条记录
- ios - Swift 5:更改 REST API 查询的 url,在 UILabel 中显示响应
- python - 了解 PyTorch LSTM 的输入形状