您的位置:程序门 -> web 开发 -> javascript



如何计算每一行的值?谢谢大家.马上解决,马上结帐


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何计算每一行的值?谢谢大家.马上解决,马上结帐[已结贴,结贴人:beijing20080808]
发表于:2007-08-05 10:22:43 楼主
在表格中
数量     价格           金额
  2           2.56      
  10         1.50
  2           5.56  
...         ...        
行数是不固定的,当输入数量和价格后,自动计算金额,在页面中,加一个文本框,自动得到总金额.
谢谢大家
发表于:2007-08-05 10:56:441楼 得分:0
在   输入框的onblur   事件中判断是否输入了数据,如果有数据,那就计算一下添加到   金额中去。
发表于:2007-08-05 11:04:332楼 得分:0
具体如何写呢
发表于:2007-08-05 11:45:093楼 得分:0
<table   style= "width:   100% "   class= "style1 "   border= "1 ">
<tr>
<td>
价格 </td>
<td> 数量 </td>
<td> 金额 </td>
</tr>
<tr>
<td> <input   name= "text4 "   type= "text "   /> </td>
<td> <input   name= "text5 "   type= "text "   onchange= "aa(this); "/> </td>
<td> <input   name= "text6 "   type= "text "   /> </td>
</tr>
<tr>
<td> <input   name= "text15 "   type= "text "   /> </td>
<td> <input   name= "text7 "   type= "text "   /> </td>
<td> <input   name= "text8 "   type= "text "   /> </td>
</tr>
<tr>
<td   style= "height:   29px "> <input   name= "text14 "   type= "text "   /> </td>
<td   style= "height:   29px "> <input   name= "text11 "   type= "text "   /> </td>
<td   style= "height:   29px "> <input   name= "text9 "   type= "text "   /> </td>
</tr>
<tr>
<td> <input   name= "text13 "   type= "text "   /> </td>
<td> <input   name= "text12 "   type= "text "   /> </td>
<td> <input   name= "text10 "   type= "text "   /> </td>
</tr>
</table>

</body>

</html>
<script   language= "javascript "   type= "text/javascript ">
function   aa(obj)
{
//debugger;
      var   obj_tx=obj.value;
      if(isnan(parsefloat(obj_tx)))
      {
            alert( "价格!!!!1 ");
      }
      else
      {
            var   tb=obj.parentnode.parentnode.parentnode.parentnode;
            var   row=obj.parentnode.parentnode.rowindex;
            var   cell=obj.parentnode.cellindex;
          var   count=tb.rows[row].cells[cell-1].childnodes[0].value;
          if(count.length==0   ¦ ¦   isnan(parseint(count)))
          {
              alert( "数量!!! ");
          }
          else
          {
                tb.rows[row].cells[cell+1].childnodes[0].value=obj_tx*count;
          }
      }
}
</script>
发表于:2007-08-05 11:57:324楼 得分:0
谢谢mary_lou(秋风),刚才我测试了一下,只有第一行能计算.
发表于:2007-08-05 12:00:535楼 得分:0
不好意思,没看清,谢谢秋风,现在只实现了每行的合计,总金额应该如何计算呢?谢谢好人
发表于:2007-08-05 12:07:546楼 得分:0
等下,我给你个例子,
发表于:2007-08-05 12:10:017楼 得分:0
问题是,你准备把总金额放在什么地方,最好把你的html贴出来,省得我模拟,快点,等你,一会要出去
发表于:2007-08-05 12:13:338楼 得分:0
好的.我的表格实际是datagrid
在页面上放个文本框.把总金额放在里面
太谢谢了
发表于:2007-08-05 12:23:479楼 得分:0
<body>
总金额: <input   id= "a1 "   name= "tt4 "   type= "text "   />
<table   style= "width:   100% "   class= "style1 "   border= "1 ">
<tr>
<td>
价格 </td>
<td> 数量 </td>
<td> 金额 </td>
</tr>
<tr>
<td> <input   name= "text4 "   type= "text "   /> </td>
<td> <input   name= "text5 "   type= "text "   onchange= "aa(this); "/> </td>
<td> <input   name= "text6 "   type= "text "   /> </td>
</tr>
<tr>
<td> <input   name= "text15 "   type= "text "   /> </td>
<td> <input   name= "text7 "   type= "text "   onchange= "aa(this); "/> </td>
<td> <input   name= "text8 "   type= "text "   /> </td>
</tr>
<tr>
<td   style= "height:   29px "> <input   name= "text14 "   type= "text "   /> </td>
<td   style= "height:   29px "> <input   name= "text11 "   type= "text "   onchange= "aa(this); "/> </td>
<td   style= "height:   29px "> <input   name= "text9 "   type= "text "   /> </td>
</tr>
<tr>
<td> <input   name= "text13 "   type= "text "   /> </td>
<td> <input   name= "text12 "   type= "text "   onchange= "aa(this); "/> </td>
<td> <input   name= "text10 "   type= "text "   /> </td>
</tr>
</table>

</body>

</html>
<script   language= "javascript "   type= "text/javascript ">
function   aa(obj)
{
//debugger;
      var   obj_tx=obj.value;
      if(isnan(parsefloat(obj_tx)))
      {
            alert( "价格!!!!1 ");
      }
      else
      {
            var   tb=obj.parentnode.parentnode.parentnode.parentnode;
            var   row=obj.parentnode.parentnode.rowindex;
            var   cell=obj.parentnode.cellindex;
          var   count=tb.rows[row].cells[cell-1].childnodes[0].value;
          if(count.length==0   ¦ ¦   isnan(parseint(count)))
          {
              alert( "数量!!! ");
          }
          else
          {
                tb.rows[row].cells[cell+1].childnodes[0].value=obj_tx*count;
                this.sum(tb);
          }
      }
}
function   sum(obj)
{
    var   result=0;
    var   row=obj.rows.length;
    for(var   i=1;i <row;i++)
    {
          var   price=   obj.rows[i].cells[2].childnodes[0].value;
          if(price.length==0)   {price=0;}
          result+=price;
    }
    document.getelementbyid( "a1 ").value=result;
}
</script>

结果好象有点问题,不过解决方法就是这样的,你再检查下
发表于:2007-08-05 12:24:4510楼 得分:0
好的.先谢谢了.
发表于:2007-08-05 12:27:2211楼 得分:0
总金额错了.呵呵
发表于:2007-08-05 12:38:4312楼 得分:35
记得给我结贴,我今天在上面赚分.跟你说不准赖账啊.
发表于:2007-08-06 10:06:0313楼 得分:0
哈哈       lz赖帐了
发表于:2007-08-06 10:29:0114楼 得分:0
lz真不厚道bs
发表于:2007-08-06 10:42:3715楼 得分:0
jf
发表于:2007-08-06 14:27:1616楼 得分:0
lz记得jt
这世道
发表于:2007-08-06 15:25:0317楼 得分:0
不好意思,没有来得及结贴.谢谢大家


快速检索

最新资讯
热门点击