0

如何在qty的动态表列中求和,我可以得到price1的总和,但是我不知道求和qty的列,这是动态表,我可以通过添加按钮添加行。请帮忙,我是php新手。

<html> <head> <div> <script type="text/javascript"> function tot(elem) { var d=document.getElementById("total").value; var total=Number(d); } var total = 0; function getValues() { var qty1 = 0; var unit = 0; var obj = document.getElementsByTagName("input"); for(var i=0; i<obj.length; i++){ if(obj[i].name == "qty1[]"){var qty1 = obj[i].value;} if(obj[i].name == "unit[]"){var unit = obj[i].value;} if(obj[i].name == "totala[]"){ if(qty1 > 0 && unit > 0) {obj[i].value = (qty1*unit).toFixed(2);total+=(obj[i].value*1);} else{obj[i].value = 0;total+=(obj[i].value*1);} } } document.getElementById("total").value = (total*1).toFixed(2); total=0; } </script> <script> function addRow(tableID) { var table = document.getElementById(tableID); var rowCount = table.rows.length; var row = table.insertRow(rowCount); var colCount = table.rows[0].cells.length; for(var i=0; i<colCount; i++) { var newcell = row.insertCell(i); newcell.innerHTML = table.rows[0].cells[i].innerHTML; //alert(newcell.childNodes); switch(newcell.childNodes[0].type) { case "text": newcell.childNodes[0].value = ""; break; case "checkbox": newcell.childNodes[0].checked = false; break; } } } function deleteRow(tableID) { try { var table = document.getElementById(tableID); var rowCount = table.rows.length; for(var i=0; i<rowCount; i++) { var row = table.rows[i]; var chkbox = row.cells[0].childNodes[0]; if (null != chkbox && true == chkbox.checked) { if (rowCount <= 1) { alert("Cannot delete all the rows."); break; } table.deleteRow(i); rowCount--; i--; } } } catch(e) { alert(e); } getValues(); } </script> </head> <body> <form id="form1" name="form1" method="post" action=""> <table width="800" border="0"> <tr> <td colspan="5" align="center">Products</td> </tr> <tr align="center"> <td><INPUT type="button" value="Add Row" onclick="addRow('dataTable')" /></td> <td> <INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" /></td> <td>&nbsp;</td> <td>&nbsp;</td> </tr> <tr align="center"> <td width="169">qty</td> <td width="145">&nbsp;</td> <td width="368">unit</td> <td width="151">price1</td> </tr> </table> <div> <table width="800" border="0" class="box4_Box" id="dataTable"> <tr> <td width="21" valign="top"><input name="chk[]" type="checkbox"/></td> <td width="280"> <input type="text" name="qty1[]" id="qty" onkeyup="getValues()"onblur=""/></td> <td width="337"> <input type="text" name="unit[]" id="unit" onkeyup="getValues()" onblur=""/></td> <td width="144"> <input type="text" name="totala[]" id="totala" onkeyup="getValues()" /></td> </tr> </table> </div> <div> <table width="800" border="0"> <tr> <td width="305"><input type="text" name="qtytotal" id="qtytotal" value="" /></td> <td width="337">&nbsp;</td> <td width="144"><input type="text" name="total" id="total" value="" /></td> </tr> </table> </div> </form> </body> </html>

4

1 回答 1

1
function sumQty(tableID) {
    var total = 0;
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    for (var i = 0; i < rowCount; i++) {
        var row = table.rows[i];
        var colCount = row.cells.length;
        for (var j = 0; j < cellCount; j++) {
            var node = row.cells[j].childNodes[0];
            if (node.name == "qty[]") {
                total += parseInt(node.value);
            }
        }
    }
    return total;
}
于 2013-03-02T11:51:15.020 回答