日期:2014-05-16  浏览次数:20496 次

动态增加行数据的自动计算?

以下是自动计算的方法
function change(e) { 
  e=e||event;
  var tag=e.target||e.srcElement;
  //用正则取出最后的数字编号,注意生成新行时的命名区别是按最后的数字来区别
   
  var str;
  if(tag.name)
  str=tag.name;
  else
  {
  alert("名称丢失!");
  return;
  }
  var reg=/[a-z]*(\d+)/i
  var m=str.match(reg);
  if(m==null)
  {
  alert("数字编号丢失!");
  return;
  }
  var NowIndex=m[1];
  var txt_hwmc, txt_jianshu,txt_danjia,txt_qsj,txt_maoli,txt_cyf,txt_zyf,txt_bxe,txt_huikou,txt_ztj,txt_zzl; 
  txt_hwmc = getValue(document.input1["hwmc"+NowIndex]);
  txt_jianshu = getValue(document.input1["jianshu"+NowIndex]);
  txt_ztj = getValue(document.input1["ztj"+NowIndex]); 
  txt_zzl = getValue(document.input1["zzl"+NowIndex]); 
  txt_danjia = getValue(document.input1["danjia"+NowIndex]); 
  txt_cyf = getValue(document.input1["cyf"+NowIndex]); 
  txt_bxe = getValue(document.input1["bxe"+NowIndex]);
  txt_huikou = getValue(document.input1["huikou"+NowIndex]);

  if((txt_jianshu!=0)&&(txt_ztj!=0))
{
txt_cyf=Math.round(txt_danjia * txt_ztj);
txt_maoli=Math.round((txt_danjia-txt_qsj)*txt_ztj);
}
else if((txt_jianshu!=0)&&(txt_zzl!=0))
{
txt_cyf=Math.round(txt_danjia * txt_zzl);
txt_maoli=Math.round((txt_danjia-txt_qsj)*txt_zzl);
}
else
{
txt_cyf=Math.round(txt_danjia * txt_jianshu);
txt_maoli=Math.round(txt_danjia-txt_qsj);
}

  bxfbl=500;

  txt_zyf = txt_cyf + (txt_bxe*1)/bxfbl + (txt_huikou * 1);  
  document.input1["ztj"+NowIndex].value = Math.round(Math.floor(( txt_ztj *10000)/10))/1000; 
  document.input1["zzl"+NowIndex].value = Math.round(Math.floor(( txt_zzl *10000)/10))/1000; 
  document.input1["cyf"+NowIndex].value = Math.round(Math.floor(( txt_cyf *10000)/10))/1000; 
  document.input1["zyf"+NowIndex].value = Math.round(Math.floor(( txt_zyf *10000)/10))/1000; 
  getTotal();



------解决方案--------------------
知道为什么吗?应为你获得到的文本框的id存在问题也就是说你没有正确的获得到他的id 实际上你完全没有必要这样写你用循环去判断就可以了,循环整个table用tableObj为已经获得到的table对象然后进行循环

for(var i=0;i<table.rows.length;i++)
{
var xObj=tableObj.rows[i].cell[0].all[0];//获得文本框对象 
}
这样做是绝对不会存在问题的,关键是看你获得的对象是不是存在问题
------解决方案--------------------
 
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<div style="height:230px;width:99.9%;overflow:auto;">

<table nowrap id="CommidityGrid" border="0" cellspacing="1" cellpadding="0" BGCOLOR="#949293" width="100%">
<Div>
<Script Language="javascript">
// 删除一行
function DeleteRow(currow) {
document.all["CommidityTypeID" + currow].value = "";
CommidityGrid.deleteRow(CommidityGrid.rows("GridRow" + currow).rowIndex);
}

function zhjs(coun){//货品折合计算
var ss="";
var mm="";
var aa=document.all["PyOrdCount"+coun].value;
var bb=document.all["PyOrdNum"+coun].value;
v