gpt4 book ai didi

javascript - DOM动态表计算

转载 作者:行者123 更新时间:2023-12-01 01:26:36 25 4
gpt4 key购买 nike

我有一个在将值输入单元格时动态计算的表格。将表中每列的第二个和第三个单元格相加以获得第四个单元格中的值,并且垂直方向上的所有值应相加以获得底部最后一个单元格的总计。有一个单元格计算不正确,即使在调试后我也无法弄清楚原因。最后一行和最后一列中的最后一个单元格计算较晚且不正确。任何帮助将不胜感激。

<table border="1" id="regionLead">
<tr>
<th colspan="4">Region Lead</th>
</tr>
<tr>
<td>Region</td>
<td>Deployed</td>
<td>Vacant</td>
<td>Total</td>
</tr>
<tr>
<td>Eastern</td>
<td><input type="text" value="0" class="column1" id="easternDeployed" onchange="test.calculate()" style="background-color:#eee;width:55px"/></td>
<td><input type="text" value="0" class="column2" id="easternVacant" onchange="test.calculate()" style="background-color:#eee;width:55px"/></td>
<td><div class="column3" id="easternTotal">0</div></td>
</tr>
<tr>
<td>Southern</td>
<td><input type="text" value="0" class="column1" id="southernDeployed" onkeydown="test.hide()" style="background-color:#eee;width:55px"/></td>
<td><input type="text" value="0" class="column2" id="southernVacant" onkeydown="test.hide()" style="background-color:#eee;width:55px"/></td>
<td><div class="column3" id="southernTotal">0</div></td>
</tr>
<tr>
<td>Central</td>
<td><input type="text" value="0" class="column1" id="centralDeployed" onkeydown="test.hide()" style="background-color:#eee;width:55px"/></td>
<td><input type="text" value="0" class="column2" id="centralVacant" onkeydown="test.hide()" style="background-color:#eee;width:55px"/></td>
<td><div class="column3" id="centralTotal">0</div></td>
</tr>
<tr>
<td>Western</td>
<td><input type="text" value="0" class="column1" id="westernDeployed" onkeydown="test.hide()" style="background-color:#eee;width:55px"/></td>
<td><input type="text" value="0" class="column2" id="westernVacant" onkeydown="test.hide()" style="background-color:#eee;width:55px"/></td>
<td><div class="column3" id="westernTotal">0</div></td>
</tr>
<tr>
<td>Europe</td>
<td><input type="text" value="0" class="column1" id="europeDeployed" onkeydown="test.hide()" style="background-color:#eee;width:55px"/></td>
<td><input type="text" value="0" class="column2" id="europeVacant" onkeydown="test.hide()" style="background-color:#eee;width:55px"/></td>
<td><div class="column3" id="europeTotal">0</div></td>
</tr>
<tr>
<td>Asia</td>
<td><input type="text" value="0" class="column1" id="asiaDeployed" onkeydown="test.hide()" style="background-color:#eee;width:55px"/></td>
<td><input type="text" value="0" class="column2" id="asiaVacant" onkeydown="test.hide()" style="background-color:#eee;width:55px"/></td>
<td><div class="column3" id="asiatotalTotal">0</div></td>
</tr>
<tr>
<td>Total</td>
<td><div id="deployed"></div></td>
<td><div id="vacant"></div></td>
<td style="background-color:yellow"><div id="total"></div></td>
</tr>
</table>


var allBoxes =
document.querySelector("#regionLead");
calculate(allBoxes);

function calculate(tbl){
tbl.addEventListener("change", function(e){
if (e.target.tagName === "INPUT"){
var rowNode = e.target.parentNode.parentNode;
//console.log(rowNode);

var rowInputs =
rowNode.getElementsByTagName('input');
var tableNode = e.target.parentNode.parentNode.parentNode;
// console.log(tableNode);
/*get Deployed column of the table to add up*/
var columnInputs = tableNode.getElementsByClassName('column1');

/*get Vacant column of the table to add up*/
var columnInputs2 = tableNode.getElementsByClassName('column2');

/*get Vacant column of the table to add up*/
var columnInputs3= tableNode.getElementsByClassName('column3');
//alert(columnInputs3.length)
var colSum = 0
for(var i=0; i<columnInputs.length; i++){
colSum += parseInt(columnInputs[i].value);
}

var colSum2 = 0
for(var i=0; i<columnInputs2.length; i++){
colSum2 += parseInt(columnInputs2[i].value);
}

var colSum3 = 0
for(var i=0; i<columnInputs3.length; i++){
console.log(parseInt(columnInputs3[i].innerText));
colSum3 += parseInt(columnInputs3[i].innerHTML);
}

//Last cell in the deployed column
tableNode.rows[8].cells[1].innerHTML = colSum;

//Last cell in the vacan column
tableNode.rows[8].cells[2].innerHTML = colSum2;

tableNode.rows[8].cells[3].innerHTML = colSum3;




var rowDiv = rowNode.getElementsByTagName('div');
var before = rowInputs[0].value || 0;
var after = rowInputs[1].value || 0;
if(before && after){
var total = parseInt(before)+parseInt(after);
rowDiv[0].innerHTML = total;
}
}
})
}

https://codepen.io/isogunro/pen/ebmrxv?editors=1011

最佳答案

试试这个:

在 Codepen 上 Javascript 代码的第 44 行中,将 colSum3 替换为 colSum + colSum2

所以第 44 行看起来像这样:

tableNode.rows[8].cells[3].innerHTML = colSum + colSum2;

这样,colSumcolSum2 就会动态添加,并且黄色网格单元会随着其他网格单元而更新。

(我仍然不明白为什么 colSum3 不使用 colSumcolSum2 更新。)

关于javascript - DOM动态表计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53716915/

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