求高手帮忙看一段JS的写法
上面一行的tr实现了功能 我想实现多行的时候该怎么做 需要怎么循环
功能就是: 在文本框输入数值后 然后后面 总价就会显示相应的数值 计算方法是: 个数* 文本框内的数值 最后3个tr的值都相应得出来后 如何算出来 总计数
HTMl代码写在里面了请高手帮忙写一个完整的多个的统计 谢谢
<!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>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('input').blur(compute)
})
function compute(){
var total=0,num,price,tr,v,rx=/^\d+(\.\d+)?$/
$(this).closest('table').find('tr:gt(0)').each(function(){
tr=$(this);
price=tr.find('input');
if(price.size()==0
[解决办法]
!rx.test(price.val())){ tr.find('span.danhang_sum').html('');return true;}
num=parseInt(tr.find('td span').html());
price=parseFloat(price.val())
v=num*price;
total+=v;
tr.find('span.danhang_sum').html(v.toFixed(2));
}).end().find('span.total_sum').html(total.toFixed(2));
}
</script>
</head>
<body>
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<th>数量</th>
<th>单价</th>
<th>总价</th>
</tr>
<tr>
<td><span id="num">3</span></td>
<td><input type="text" ></td>
<td><span class="danhang_sum"></span></td>
</tr>
<tr>
<td colspan="2"><span style="color:red">总计:</span></td>
<td><span style="color:red">¥</span><span class="total_sum"></span></td>
</tr>
</table>
<br />
<br />
<br />
<hr>
<br />
<br />
<br />
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<th>数量</th>
<th>单价</th>
<th>总价</th>
</tr>
<tr>
<td><span id="num">3</span></td>
<td><input type="text" ></td>
<td><span class="danhang_sum"></span></td>
</tr>
<tr>
<td><span id="num">8</span></td>
<td><input type="text" ></td>
<td><span class="danhang_sum"></span></td>
</tr>
<tr>
<td><span id="num">1</span></td>
<td><input type="text" ></td>
<td><span class="danhang_sum"></span></td>
</tr>
<tr>
<td colspan="2"><span style="color:red">总计:</span></td>
<td><span style="color:red">¥</span><span class="total_sum"></span></td>
</tr>
</table>
</body>
</html>