日期:2014-05-17 浏览次数:20712 次
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script type="text/javascript"> window.onload = function() { var obj = document.getElementsByTagName('table')[0].getElementsByTagName('tr')[1].getElementsByTagName('input'); for (var i = 0; i < obj.length - 1; i ++) { obj[i].onkeyup = function() { var sum = 0; for (var j = 0; j < obj.length - 1; j ++) { if (obj[j].value.length < 1 || parseInt(obj[j].value) == 'NaN') sum += 0; else sum += parseInt(obj[j].value); } obj[4].value = sum; } } } </script> </head> <body> <table width="500" border="1"> <tr> <td>第一个数</td> <td>第二个数</td> <td>第三个数</td> <td>第四个数</td> <td>总和</td> </tr> <tr> <td><input type="text" name="num1" /></td> <td><input type="text" name="num2" /></td> <td><input type="text" name="num3" /></td> <td><input type="text" name="num4" /></td> <td><input type="text" name="he" value="0" /></td> </tr> </table> </body> </html>