简体   繁体   English

在javascript中获取动态添加行的计算

[英]get calculation on dynamic add row in javascript

Im having a problem with my input box not showing any values.我的输入框有问题,没有显示任何值。

 var renumber = 0; function addItem() { renumber++; var html = "<tr"; html += "<td id='itemNum'>" + renumber + "</td>"; html += "<td><input name='itemName[]'></td>"; html += "<td><input name='itemDescription[]'></td>"; html += "<td><span class='currency'>$</span><input id='perHour' value='0' name='amountPerHour[]'></td>"; html += "<td><input id='lineHours' value='0' name='hours[]'></td>"; html += "<td><span class='currency'>$</span><input id='lineTotal' onblur='lineTotal(this);' value='0' name='lineTotal[]'></td>"; html += "<td><button type='button' id='remove_button' onclick='removeItem();'> X </button></td>"; html += "</tr>"; document.getElementById("addItems").insertRow().innerHTML = html; } function removeItem() { document.getElementById("addItems").deleteRow(0); renumber--; var reorder = tblRow.rows; for(var i = 0; i < reorder.length; i++) { renumber[i]; renumber++; } document.getElementById("itemNum").innerHTML = renumber; } function lineTotal(elem) { var mainRow = document.getElementById(elem); var AmtPerHour = mainRow.getElementsByTagName('td').getElementById('perHour')[0].value; var lnHrs = mainRow.getElementsByTagName('td').getElementById('lineHours')[0].value; var total = mainRow.getElementsByTagName('td').getElementById('lineTotal')[0]; var myResult = AmtPerHour * lnHrs; total.value = myResult; }
 <table> <tr> <th>#</th> <th>Item Name</th> <th>Item Description</th> <th>Amount Per Hour</th> <th>Total Hours</th> <th>Line Total</th> </tr> <tbody id="addItems"></tbody> </table> <p> <button type="button" onclick="addItem();"> Add Item </button> </p> <p> Amount Due: $0.00 <script type="text/javascript"> //add the amounts from all items. //if none added then have it set to zero. </script> </p> <p> Due Date: <script type="text/javascript"> //start it 2 weeks from actual date </script> </p> <p> <input id="invoice_submit" type="submit" name="submit"> </p>

my biggest concern is the values not appearing.我最担心的是值没有出现。

Second problem, but one I can live with:第二个问题,但我可以忍受:
For rows, row1, row2, row3.对于行,row1、row2、row3。 if I delete row2, I would like it to then be row1, row2.如果我删除row2,我希望它是row1,row2。
Right now it is row1, row3.现在是第 1 行,第 3 行。 I am talking about the # table section.我说的是# table 部分。

any ideas?有任何想法吗?

Hi please find below working snippet solving your first and second problems.嗨,请在下面找到解决您的第一和第二个问题的工作片段。 I made a few fixes to your code.我对您的代码进行了一些修复。

  1. your data was not showing because getElementById expects you to pass element ID as string but you were passing element to it so this document.getElementById(elem) wont work.您的数据未显示,因为getElementById期望您将元素 ID 作为字符串传递,但您将元素传递给它,因此此document.getElementById(elem)不起作用。

  2. you were calculating line total when that field is getting blurred but you should calculate line total also when user makes changes to either amount per hour or total hours so I added onblur to those fields also.当该字段变得模糊时,您正在计算行总数,但是当用户更改amount per hourtotal hours amount per hour时,您也应该计算行总数,因此我也将 onblur 添加到这些字段中。

  3. your delete row was deleting the first row always no matter which row I am trying to delete ( document.getElementById("addItems").deleteRow(0); will always delete the first row in table ) so I added the row reference to that function and now it deletes the appropriate row.无论我要删除哪一行,您的删除行总是删除第一行( document.getElementById("addItems").deleteRow(0);将始终删除表中的第一行)所以我添加了行引用函数,现在它删除适当的行。

  4. I added a Due Amount Total method to your Javascript to calculate due amount every time table is updated(insert/delete row) and updates the label value Amount Due: $0.00我在您的 Javascript 中添加了到期金额总计方法来计算每次更新表时的到期金额(插入/删除行)并更新标签值到期金额:$0.00

  5. I added the logic to renumber all the rows whenever a row is deleted.我添加了在删除行时对所有行重新编号的逻辑。 ( your second problem is no more so dont live with it :) ). 你的第二个问题不再存在,所以不要忍受它:))。

 var renumber = 0; // trigger everytime there is change in the table row to calculate the new due amount. function calculateDueAmount() { var tblRows = document.getElementById("addItems").getElementsByTagName('tr'); let total = 0; for (var i = 0; i < tblRows.length; i++) { let lineTotal = tblRows[i].getElementsByTagName('td')[5].getElementsByTagName('input')[0].value; total += Number(lineTotal) } document.getElementById("amountDue").innerText = total.toFixed(2); } // no changes required in this function addItem() { renumber++; var html = "<tr>"; html += "<td id='itemNum'>" + renumber + "</td>"; html += "<td><input name='itemName[]'></td>"; html += "<td><input name='itemDescription[]'></td>"; html += "<td><span class='currency'>$</span><input id='perHour' onblur='lineTotal(this);' value='0' name='amountPerHour[]'></td>"; html += "<td><input id='lineHours' onblur='lineTotal(this);' value='0' name='hours[]'></td>"; html += "<td><span class='currency'>$</span><input id='lineTotal' onblur='lineTotal(this);' value='0' name='lineTotal[]'></td>"; html += "<td><button type='button' id='remove_button' onclick='removeItem(this);'> X </button></td>"; html += "</tr>"; document.getElementById("addItems").insertRow().innerHTML = html; } // updated and sanitized the logic to delete the row function removeItem(elem) { let rowToDelete = elem.parentElement.parentElement; let rowNumber = rowToDelete.getElementsByTagName('td')[0].innerText; document.getElementById("addItems").deleteRow(rowNumber - 1); let tblRows = document.getElementById('addItems').getElementsByTagName('tr'); let j = 0; for (; j < tblRows.length; j++) { tblRows[j].getElementsByTagName('td')[0].innerText = j + 1 } calculateDueAmount(); // calculate due amount since row got deleted. renumber = j; } function lineTotal(elem) { var mainRow = elem.parentElement.parentElement; var AmtPerHour = mainRow.getElementsByTagName('td')[3].getElementsByTagName('input')[0].value; var lnHrs = mainRow.getElementsByTagName('td')[4].getElementsByTagName('input')[0].value; var total = mainRow.getElementsByTagName('td')[5].getElementsByTagName('input')[0]; var myResult = Number(AmtPerHour) * Number(lnHrs); total.value = myResult; calculateDueAmount(); // calculate due amount since row got added }
 <table> <tr> <th>#</th> <th>Item Name</th> <th>Item Description</th> <th>Amount Per Hour</th> <th>Total Hours</th> <th>Line Total</th> </tr> <tbody id="addItems"></tbody> </table> <p> <button type="button" onclick="addItem();"> Add Item </button> </p> <p> Amount Due: $<span id="amountDue">0.00</span> <script type="text/javascript"> //add the amounts from all items. //if none added then have it set to zero. </script> </p> <p> Due Date: <script type="text/javascript"> //start it 2 weeks from actual date </script> </p> <p> <input id="invoice_submit" type="submit" name="submit"> </p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM