# JS求乘积，求和

0

JS自动求和

cnns | 初学一级 | 园豆：1

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">
<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>
<body>

<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

ThreeTree | 小虾三级 |园豆：1490 | 2013-12-25 22:25

cnns | 园豆：1 (初学一级) | 2013-12-26 09:39

0

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;
})
}```

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

0

您需要登录以后才能回答，未注册用户请先注册