(谢谢各位,重新开贴发问)从数字一到数字五相加得到结果,给与数字六.代码如下.怎么不行? cell从0开始算,按照你的TABLE应该是从4开始 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 sorry,我的错,原来的代码只适用于1行,现在可以了------------------------------------------------------<script type="text/javascript">var table = document.getElementById("tb1");for(var i=1;i<table.rows.length;i++){ var row = table.rows[i]; row.cells[9].firstChild.readOnly="true"; row.cells[4].firstChild.onchange=row.cells[5].firstChild.onchange=row.cells[6].firstChild.onchange=row.cells[7].firstChild.onchange=row.cells[8].firstChild.onchange=function(){ var crow = this.parentElement.parentElement; crow.cells[9].firstChild.value=crow.cells[4].firstChild.value*1 + crow.cells[5].firstChild.value*1 + crow.cells[6].firstChild.value*1 + crow.cells[7].firstChild.value*1 + crow.cells[8].firstChild.value*1; }}</script> to: leohuang(LEO)怎么用了你改的script还是不行啊?好像没触第九个列的值的改变?能把我的代码拷下来测测么?不知是我哪错. 恩.又可以了.是不是script要放在后尾才行? <table id="tb1" name="tb1" border=1><tr><td>开头一 </td><td>开头二</td><td>开头三</td><td>开头四 </td><td>数字一</td><td>数字二</td><td>数字三</td><td>数字四</td><td>数字五</td><td>数字六(总和)</td></tr><tr><td>开头一</td><td>开头二</td><td>开头三</td><td>开头四</td><td><input type="text"></td><td><input type="text"></td><td><input type="text"></td><td><input type="text"></td><td><input type="text"></td><td><input type="text"></td></tr><tr><td>开头一</td><td>开头二</td><td>开头三</td><td>开头四</td><td><input type="text"></td><td><input type="text"></td><td><input type="text"></td><td><input type="text"></td><td><input type="text"></td><td><input type="text"></td></tr></table><script type="text/javascript">var table = document.getElementById("tb1");for(var i=1;i<table.rows.length;i++){ var row = table.rows[i]; row.cells[9].firstChild.readOnly="true"; row.cells[5].firstChild.onchange= row.cells[6].firstChild.onchange= row.cells[7].firstChild.onchange= row.cells[8].firstChild.onchange= row.cells[9].firstChild.onchange= function(){ row.cells[9].firstChild.value= row.cells[5].firstChild.value*1 + row.cells[6].firstChild.value*1 + row.cells[7].firstChild.value*1 + row.cells[8].firstChild.value*1}}</script> 对,也可以把那段代码写成一个function然后<script type="text/javascript">window.onload=functionName;</script>楼上的应该是45678=function(){9=45678} 来晚了 没办法早上工作太忙了没时间写 算了写都写了不贴出来也不行说明:1.需要要获取Table元素2.不管怎么样Table中每一行只要有2或2个以上的<input type="text">就可以执行3.最后一个<input>存放本行的所有<input>的值的累加(只累加数字)<script language="javascript">function window.onload(){ var tableobj = document.getElementById("tb1");//获取Table元素 for(var i=0;i<tableobj.rows.length;i++) { setTr(tableobj.rows[i]); }}function setTr(inputTr){ if(inputTr.tagName == "TR") { var inputs = inputTr.getElementsByTagName("input"); var aa = new CountGroup(); for(var i=0;i<inputs.length;i++) { var tempobj = inputs[i] if(tempobj.type.toUpperCase()=="TEXT") { aa.Items.push(tempobj); tempobj.onchange = function(){aa.count();} } } if(aa.Items.length>1) { aa.source = aa.Items.pop(); } }}function CountGroup(){ this.source = null; this.Items = new Array();}CountGroup.prototype.count = function() { var value = 0; for(var i=0;i<this.Items.length;i++) { var tempint = parseInt(this.Items[i].value); value += isNaN(tempint)?0:tempint; } this.source.value = value;}</script> 关于ExtJS中TreeLoader或Store请求问题 JavaScript 控制 Media Player 播放进度的问题 表单验证 ajax FckEditor上传图片乱码? CSDN社区,左边的这个树型是什么实现的?求助,在线等 对于会者很简单的一个问题 jqGrid 多选提交的问题? 请教javascript中有没有类似vbs中的doEvents的语句 高分求巨难菜单源码! JS将一个对象赋值给另外一个变量,是地址传值吗? 在JS中,我如何做到选择一个文本框里面指定的字符串? 如何在新打开的窗口中加载object.innerHTML()内的CSS?
------------------------------------------------------
<script type="text/javascript">
var table = document.getElementById("tb1");
for(var i=1;i<table.rows.length;i++){
var row = table.rows[i];
row.cells[9].firstChild.readOnly="true";
row.cells[4].firstChild.onchange=row.cells[5].firstChild.onchange=row.cells[6].firstChild.onchange=row.cells[7].firstChild.onchange=row.cells[8].firstChild.onchange=function(){
var crow = this.parentElement.parentElement;
crow.cells[9].firstChild.value=crow.cells[4].firstChild.value*1 + crow.cells[5].firstChild.value*1 + crow.cells[6].firstChild.value*1 + crow.cells[7].firstChild.value*1 + crow.cells[8].firstChild.value*1;
}
}
</script>
<tr><td>开头一 </td><td>开头二</td><td>开头三</td><td>开头四 </td><td>数字一</td><td>数字二</td><td>数字三</td><td>数字四</td><td>数字五</td><td>数字六(总和)</td></tr>
<tr>
<td>开头一</td>
<td>开头二</td>
<td>开头三</td>
<td>开头四</td>
<td><input type="text"></td>
<td><input type="text"></td>
<td><input type="text"></td>
<td><input type="text"></td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr><tr>
<td>开头一</td>
<td>开头二</td>
<td>开头三</td>
<td>开头四</td>
<td><input type="text"></td>
<td><input type="text"></td>
<td><input type="text"></td>
<td><input type="text"></td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
</table><script type="text/javascript">
var table = document.getElementById("tb1");
for(var i=1;i<table.rows.length;i++){
var row = table.rows[i];
row.cells[9].firstChild.readOnly="true";
row.cells[5].firstChild.onchange=
row.cells[6].firstChild.onchange=
row.cells[7].firstChild.onchange=
row.cells[8].firstChild.onchange=
row.cells[9].firstChild.onchange=
function(){
row.cells[9].firstChild.value=
row.cells[5].firstChild.value*1 +
row.cells[6].firstChild.value*1 +
row.cells[7].firstChild.value*1 +
row.cells[8].firstChild.value*1}
}
</script>
然后
<script type="text/javascript">
window.onload=functionName;
</script>楼上的应该是45678=function(){
9=45678
}
说明:
1.需要要获取Table元素
2.不管怎么样Table中每一行只要有2或2个以上的<input type="text">就可以执行
3.最后一个<input>存放本行的所有<input>的值的累加(只累加数字)<script language="javascript">
function window.onload()
{
var tableobj = document.getElementById("tb1");//获取Table元素
for(var i=0;i<tableobj.rows.length;i++)
{
setTr(tableobj.rows[i]);
}
}function setTr(inputTr)
{
if(inputTr.tagName == "TR")
{
var inputs = inputTr.getElementsByTagName("input");
var aa = new CountGroup();
for(var i=0;i<inputs.length;i++)
{
var tempobj = inputs[i]
if(tempobj.type.toUpperCase()=="TEXT")
{
aa.Items.push(tempobj);
tempobj.onchange = function(){aa.count();}
}
}
if(aa.Items.length>1)
{
aa.source = aa.Items.pop();
}
}
}function CountGroup()
{
this.source = null;
this.Items = new Array();
}CountGroup.prototype.count = function()
{
var value = 0;
for(var i=0;i<this.Items.length;i++)
{
var tempint = parseInt(this.Items[i].value);
value += isNaN(tempint)?0:tempint;
}
this.source.value = value;
}
</script>