日期:2014-05-16 浏览次数:20311 次
<script type="text/javascript"> function tiJiao(){ var id_td1 = document.getElementById("td1"); var id_td2 = document.getElementById("td2"); var id_td3 = document.getElementById("td3"); id_td3.innerHTML =id_td1.innerHTML * id_td2.innerHTML; } function add(id) { var row = id.insertRow(id.rows.length); var col = row.insertCell(0); col.innerHTML = " "; var col = row.insertCell(1); col.innerHTML = " "; col = row.insertCell(2); col.innerHTML = " "; } </script> <table border="1" id="tbid"> <tr> <td width="100px;">单价</td> <td width="100px;">数量</td> <td width="100px;">总额</td> </tr> <tr> <td id="td1">12</td> <td id="td2">34</td> <td id="td3"></td> </tr> </table> <input type="button" onClick="tiJiao()" value="计算"> <input type="button" onClick="add(tbid)" value="增加行">
------解决方案--------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <table width="80%" border="1" align="center" id="test"> <tr> <td width="69">列值</td> <td width="69">整数一</td> <td width="69">整数二</td> <td width="54">总和</td> </tr> <tr> <td>1</td> <td><input type="text" name="textfield" onchange="count(this)"></td> <td><input type="text" name="textfield3" onchange="count(this)"></td> <td><input type="text" name="textfield5" disabled></td> </tr> <tr> <td>2</td> <td><input type="text" name="textfield2" onchange="count(this)"></td> <td><input type="text" name="textfield4" onchange="count(this)"></td> <td><input type="text" name="textfield6"disabled></td> </tr> <tr> <td>合计</td> <td><input type="text" name="textfield2" disabled></td> <td><input type="text" name="textfield4" disabled></td> <td><input type="text" name="textfield6" disabled></td> </tr> </table> <script type="text/javascript"> function count(inp) { //算总和 var trObj = inp.parentElement.parentElement; var sum = 0; for (var i=1; i<trObj.children.length-1; i++) //循环input所在行的单元格(除去第一格和最后一格) { sum += trObj.childNodes[i].firstChild.value ? parseInt(trObj.childNodes[i].firstChild.value,10) : 0; //如果input值为空,则算作0 } trObj.childNodes[trObj.children.length-1].firstChild.value = sum; //给最后一格内的input赋值 //算合计 var tab = document.getElementById("test"); for (var i=1; i<tab.rows[0].cells.length; i++) //循环列(除去第一列和最后一列) { var total = 0; for (var j=1; j<t