使用javascript计算动态添加行中最后一列的总和

我有一个表,用户可以根据需要动态添加一行。我需要在表格下方添加一个文本框,该文本框将使用 JavaScript 动态输出最后一列的总数。如果无法动态完成计算,那么我可以在文本框下方添加一个计算按钮


<HTML>

<HEAD>

    <TITLE> Add/Remove dynamic rows in HTML table </TITLE>

 

    <SCRIPT language="javascript">

function addRow(tableID) {

    var table = document.getElementById(tableID);

    var rowCount = table.rows.length;

    if (rowCount < 4) { // limit the user from creating fields more than your limits

        var row = table.insertRow(rowCount);


        var colCount = table.rows[0].cells.length;

        row.id = 'row_'+rowCount;

        for (var i = 0; i < colCount; i++) {

            var newcell = row.insertCell(i);

            newcell.outerHTML = table.rows[0].cells[i].outerHTML;            

        }

       var listitems= row.querySelectorAll("input, select");

       

            for (i=0; i<listitems.length; i++) {

              listitems[i].setAttribute("oninput", "calculate('"+row.id+"')");

            }

          

    } else {

        alert("Maximum Passenger per ticket is 4.");


    }

}

function calculate(elementID) {

    var mainRow = document.getElementById(elementID);

    var myBox1 = mainRow.querySelectorAll('[name=qty]')[0].value;

    var myBox3 = mainRow.querySelectorAll('[name^=sel]')[0].value;

    var total = mainRow.querySelectorAll('[name=total]')[0];

    var myResult1 = myBox1 * myBox3;

    total.value = myResult1;


}

任何帮助将不胜感激。


慕斯王
浏览 158回答 2
2回答

Helenr

在这里试试这个。我首先添加了总和tfoot,但是添加新行的方式让它变得很尴尬,所以我只是把它放在表格底部的一个 div 中。<html>&nbsp; <head>&nbsp; &nbsp; <title>Add/Remove dynamic rows in HTML table</title>&nbsp; &nbsp; <script language="javascript">&nbsp; &nbsp; &nbsp; function addRow(tableID) {&nbsp; &nbsp; &nbsp; &nbsp; var table = document.getElementById(tableID);&nbsp; &nbsp; &nbsp; &nbsp; var rowCount = table.rows.length;&nbsp; &nbsp; &nbsp; &nbsp; if (rowCount < 4) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // limit the user from creating fields more than your limits&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var row = table.insertRow(rowCount);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var colCount = table.rows[0].cells.length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row.id = "row_" + rowCount;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < colCount; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var newcell = row.insertCell(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newcell.outerHTML = table.rows[0].cells[i].outerHTML;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var listitems = row.querySelectorAll("input, select");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (i = 0; i < listitems.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listitems[i].setAttribute("oninput", "calculate('" + row.id + "')");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("Maximum Passenger per ticket is 4.");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; function calculate(elementID) {&nbsp; &nbsp; &nbsp; &nbsp; var mainRow = document.getElementById(elementID);&nbsp; &nbsp; &nbsp; &nbsp; var myBox1 = mainRow.querySelectorAll("[name=qty]")[0].value;&nbsp; &nbsp; &nbsp; &nbsp; var myBox3 = mainRow.querySelectorAll("[name^=sel]")[0].value;&nbsp; &nbsp; &nbsp; &nbsp; var total = mainRow.querySelectorAll("[name=total]")[0];&nbsp; &nbsp; &nbsp; &nbsp; var myResult1 = myBox1 * myBox3;&nbsp; &nbsp; &nbsp; &nbsp; total.value = myResult1;&nbsp; &nbsp; &nbsp; &nbsp; // calculate the totale of every total&nbsp; &nbsp; &nbsp; &nbsp; var sumContainer = document.getElementById("totalOfTotals");&nbsp; &nbsp; &nbsp; &nbsp; var totalContainers = document.querySelectorAll("[name=total]"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i;&nbsp; &nbsp; &nbsp; &nbsp; var sumValue = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (i = 0; i < totalContainers.length; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sumValue += parseInt(totalContainers[i].value);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; sumContainer.textContent = sumValue;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </script>&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; <input type="button" value="Add" onClick="addRow('dataTable')" />&nbsp; &nbsp; <table id="dataTable" class="form" border="1">&nbsp; &nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; &nbsp; <tr id="row_0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Quantity</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type="number"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; required="required"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name="qty"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oninput="calculate('row_0')"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="sel">Price</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <select name="sel" id="sel" oninput="calculate('row_0')" required>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="" disabled selected>Choose your option</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="1">1</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="2">2</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="3">3</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="4">4</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </select>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="total">Total</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type="text"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; required="required"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class="small"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name="total"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; </tbody>&nbsp; &nbsp; </table>&nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <span>Sum</span>&nbsp; &nbsp; &nbsp; &nbsp; <span id="totalOfTotals">0</span>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </div>&nbsp; </body></html>

BIG阳

我相信您想获得整个表中最后一列的总值。然后我认为您需要迭代列。使用下面的功能代码。function totalvalues() {&nbsp; var table = document.getElementById("dataTable");&nbsp; var totalcellvalue = 0;&nbsp; for (var i = 0, row; row = table.rows[i]; i++) {&nbsp; &nbsp; //rows would be accessed using the "row" variable assigned in the for loop&nbsp; &nbsp; for (var j = 0, col; col = row.cells[j]; j++) {&nbsp; &nbsp; &nbsp; //columns would be accessed using the "col" variable assigned in the for loop&nbsp; &nbsp; &nbsp; if (j == 2) {&nbsp; &nbsp; &nbsp; &nbsp; //alert('col html>>'+col.children[1].value);&nbsp; &nbsp; &nbsp; &nbsp; totalcellvalue += parseInt(col.children[1].value);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }&nbsp; console.log(totalcellvalue);}// And I have called the above method ```totalvalues()`` in your ```calculate()``` method.<HTML><HEAD>&nbsp; <TITLE> Add/Remove dynamic rows in HTML table </TITLE>&nbsp; <SCRIPT language="javascript">&nbsp; &nbsp; function addRow(tableID) {&nbsp; &nbsp; &nbsp; var table = document.getElementById(tableID);&nbsp; &nbsp; &nbsp; var rowCount = table.rows.length;&nbsp; &nbsp; &nbsp; if (rowCount < 4) { // limit the user from creating fields more than your limits&nbsp; &nbsp; &nbsp; &nbsp; var row = table.insertRow(rowCount);&nbsp; &nbsp; &nbsp; &nbsp; var colCount = table.rows[0].cells.length;&nbsp; &nbsp; &nbsp; &nbsp; row.id = 'row_' + rowCount;&nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < colCount; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var newcell = row.insertCell(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newcell.outerHTML = table.rows[0].cells[i].outerHTML;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; var listitems = row.querySelectorAll("input, select");&nbsp; &nbsp; &nbsp; &nbsp; for (i = 0; i < listitems.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listitems[i].setAttribute("oninput", "calculate('" + row.id + "')");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; alert("Maximum Passenger per ticket is 4.");&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; function calculate(elementID) {&nbsp; &nbsp; &nbsp; var mainRow = document.getElementById(elementID);&nbsp; &nbsp; &nbsp; var myBox1 = mainRow.querySelectorAll('[name=qty]')[0].value;&nbsp; &nbsp; &nbsp; var myBox3 = mainRow.querySelectorAll('[name^=sel]')[0].value;&nbsp; &nbsp; &nbsp; var total = mainRow.querySelectorAll('[name=total]')[0];&nbsp; &nbsp; &nbsp; var myResult1 = myBox1 * myBox3;&nbsp; &nbsp; &nbsp; total.value = myResult1;&nbsp; &nbsp; &nbsp; totalvalues();// calling my function here&nbsp; &nbsp; }&nbsp; </SCRIPT></HEAD><BODY>&nbsp; <input type="button" value="Add" onClick="addRow('dataTable')" />&nbsp; <table id="dataTable" class="form" border="1">&nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; <tr id='row_0'>&nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Quantity</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="number" required="required" name="qty" oninput="calculate('row_0')">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="sel">Price</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <select name="sel" id="sel" oninput="calculate('row_0')" required>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="" disabled selected>Choose your option</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="1">1</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="2">2</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="3">3</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="4">4</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </select>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="total">Total</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="number" required="required" class="small" name="total">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </tbody>&nbsp; </table></BODY></HTML>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript