function getTotal()
{
var tmp=0;
for(i=1;i<tab2.rows.length;i++)
{
tmp =tmp+isNaN(tab2.rows[i].cells[1].innerHTML);
}
document.all.real.value=tmp;
return true;
}我这样得到的为什么不是单元格中的值之和,而是行的总数
{
var tmp=0;
for(i=1;i<tab2.rows.length;i++)
{
tmp =tmp+isNaN(tab2.rows[i].cells[1].innerHTML);
}
document.all.real.value=tmp;
return true;
}我这样得到的为什么不是单元格中的值之和,而是行的总数
既然你用了<input type=text为什么还要去取innseHtml呢?那取出还是<input type=text name="row[]" value="0" onmouseover="this.focus()">啊!
function sumall(len) ////传入输入的列数目
{
var obj;
var sum=0;
for (var i=0;i<len;i++)
{
obj=eval("document.myform.elements['"+i+"']");
sum+=eval(obj.value);
}
alert(sum) ;
}
</script>
<form name="myform">
<input type="text" name="row[]" value="2">
<input type="text" name="row[]" value="3">
<input type="button" onclick="sumall(2)"
</form>
我得不到结果<?
include("config.php");
?>
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<LINK href="home.css" rel=stylesheet>
<title>结帐</title>
<style>
input.smallInput{
border:1 solid black;FONT-SIZE: 9pt; FONT-STYLE: normal; FONT-VARIANT: normal; FONT-WEIGHT: normal; HEIGHT: 18px; LINE-HEIGHT: normal
}
.TextBoxStyle {
border:1 solid black;FONT-SIZE: 9pt; FONT-STYLE: normal; FONT-VARIANT: normal; FONT-WEIGHT: normal; HEIGHT: 18px; LINE-HEIGHT: normal;background-color: #FFFFCC
}.TextBoxStyle1 {
background-color: Transparent;border:1 solid black;FONT-SIZE: 9pt; FONT-STYLE: normal; FONT-VARIANT: normal; FONT-WEIGHT: normal; HEIGHT: 18px; LINE-HEIGHT: normal
}
</style>
<script>
var objTemp;
function showColor(obj)
{
if (obj != objTemp && objTemp!= null)
{
objTemp.className="TextBoxStyle1";
}
obj.className="TextBoxStyle";//点击输入框需要变换的底色
objTemp = obj;
}
</script>
</head><body>
<center>
<form name="frmpay" method="post" action="<?echo $PHP_SELF?>" target="_self">
<br><br>
<table border="0" width="50%" >
<tr>
<td width="50%">合计:<?echo $costtotal?></td>
</tr>
</table>
<table border="0" width="95%" height=200>
<tr>
<td valign=top width=40% align=right>
<table id=tab1 border=0 cellspacing=1 class=tab bgcolor=#CCCCCC>
<tr bgcolor=yellow><td width=100 align=center>付款方式</td></tr>
<script>
<?
$sql = "select * from paytype order by id";
$query = mysql_query($sql);
while($result=mysql_fetch_array($query))
{
echo "document.write('<tr bgcolor=white onclick=add(this) onmouseout=this.bgColor=\"white\" onmouseover=this.bgColor=\"#efffff\"><td align=center>".$result[type]."</td></tr></tr>');\n";
}
?>
</script>
</table>
</td>
<td width=10%> </td>
<td width=50% valign=top align=left>
<table id=tab2 border=0 cellspacing=1 bgcolor=gray>
<tr bgcolor=redyellow id=tr1><td width=100 align=center>付款方式</td><td width=100 align=center>金额</td></tr>
</table>
</td>
</tr>
</table>
<table border="0" width="95%" height=200>
<tr>
<td valign=top width=40% align=right> </td>
<td width=10%> </td>
<td width=50% valign=top align=left>
<hr>
实收:<input name=real value="" readonly size=10><br><br>
找零:<input name=back value="" readonly size=10>
</td>
</tr>
</table><script>
function add(obj)
{
//tab1.setExpression
tr=tab2.insertRow() tr.style.backgroundColor="white"
tr.onmouseout=function(){this.bgColor="white"}
tr.onmouseover=function(){this.bgColor="#efffff"}
tr.ondblclick=function(){add2(this)}
tr.insertCell(0).innerHTML=obj.cells(0).innerHTML
//alert(tab2.rows.length);
tmpstr="tr.insertCell(1).innerHTML='<input class=smallInput name=type"+tab2.rows.length+" value=0 size=12 onfocus=\"javascript:showColor(this)\" onclick=\"this.focus()\">'";
alert(tmpstr);
eval(tmpstr);
//alert(obj.rowIndex);
//tab1.deleteRow(obj.rowIndex)
}
function add2(o)
{
//tr=tab1.insertRow()
tr.style.backgroundColor="white"
tr.onmouseout=function(){this.bgColor="white"}
tr.onmouseover=function(){this.bgColor="#efffff"}
tr.ondblclick=function(){add(this)}
//tr.insertCell(0).innerHTML=o.cells(0).innerHTML
tab2.deleteRow(o.rowIndex)
}
function getTotal()
{
var a;
var sum=0;
for (var i=2;i<tab2.rows.length;i++)
{
numstr="document.all.type['"+i+"']";
a=eval(numstr);
sum+=eval(a.value);
}
if(window.event.keyCode==13)
{ document.all.real.value=sum;
alert(sum);
}
}
document.onkeydown=getTotal();
</script> <p><input type="submit" value="提交" name="B1" disabled></p>
<p> </td>
</tr>
</table>
</div>
</body>
</html>