首页 新闻 会员 周边

JS求乘积,求和

0
悬赏园豆:15 [已解决问题] 解决于 2013-12-26 09:40

JS自动求和

单价*数量+第二个表格的单价*数量的总和自动计算到总和里面

如果点添加按钮继续添加两个文本框 这样就计算三个表格的总和

以此类推

cnns的主页 cnns | 初学一级 | 园豆:1
提问于:2013-12-25 11:51
< >
分享
最佳答案
0
<!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>
<style type="text/css">

</style>
<script type="text/javascript" language="javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
    $(function() {
        $("#panel :text").change(function() {
            var totals = 0;
            $("#panel").find("tr").each(function() {
                var $this = $(this);
                var price = $this.find("input.price").val();
                var num = $this.find("input.num").val();
                price = parseInt(price);
                num = parseInt(num);
                if(!(isNaN(price) || isNaN(num))) {
                    totals += (price * num);
                }
            });
            $("#total").val(totals);
        });

        $("#panel :button").click(function() {
            //var $tr = $("#panel").find("tr").first().clone(true);
            //$("#panel").append($tr);
            //------------------------------------
            //var $tr = $(this).parent().parent();
            //$tr.after($tr.clone(true));
            //------------------------------------
            var $tr = $("#ele").clone(true);
            $tr.find("input.price").val("");
            $tr.find("input.num").val("");
            $("#panel").append($tr);
        });
    })
</script>
</head>
<body>
总和:<input type="text" id="total" />
<table id="panel">
    <tr id="ele">
        <td>单价:</td>
        <td><input type="text" class="price" /></td>
        <td>数量:</td>
        <td><input type="text" class="num" /></td>
        <td><input type="button" value="添加" /></td>
    </tr>
    <tr>
        <td>单价:</td>
        <td><input type="text" class="price" /></td>
        <td>数量:</td>
        <td><input type="text" class="num" /></td>
        <td><input type="button" value="添加" /></td>
    </tr>
</table>
</body>
</html>
View Code

试试看,不知道是不是你想要的,正如楼上所说,最好提供部分html代码

收获园豆:15
ThreeTree | 小虾三级 |园豆:1490 | 2013-12-25 22:25

谢谢 大体就是这个意思

cnns | 园豆:1 (初学一级) | 2013-12-26 09:39
其他回答(3)
0

分别获取单价的value和数量的value,parseInt之后,就可以计算了

秋壶冰月 | 园豆:5903 (大侠五级) | 2013-12-25 12:14
0
function()
{
   var totalPrice
  $("table tr").each(function(){
      var price = parseInt($(this).find(".txtPrice").val());
var num =parseInt($(this).find(".txtNum").val());
totalPrice+= price*num;
}) alert(totalPrice); }

 

添加的文本框都是在 table容器的 tr 里的,然后遍历tr里的 价格与 数量 

至于这个函数什么时候调用就由你自己决定了 

Zery | 园豆:6151 (大侠五级) | 2013-12-25 12:33

这么算不对吧? 不能对单价和数量先求和。2×2+3×3 明显不等于(2+3)×(2+3)

支持(0) 反对(0) sam.c | 园豆:148 (初学一级) | 2013-12-25 13:50

这种是否有问题?  1.5*2 +1*3  是否就和 2.5*5 结果一样呢? 你这种方式 我表示无语

支持(0) 反对(0) wolfy | 园豆:2636 (老鸟四级) | 2013-12-25 13:58
0

这种问题,首先建议你查询下Jquery的api,很容易就能实现。

再者,如果你需要代码帮助理解,你可以提供部分html代码,至少可以让其他人针对性的写出合适的代码。

幻天芒 | 园豆:37175 (高人七级) | 2013-12-25 20:31
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册