欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jQuery实现购物车多物品数量的加减+总价计算

程序员文章站 2022-06-21 08:25:42
jquery实现购物车多物品数量的加减+总价计算代码如下:

jquery实现购物车多物品数量的加减+总价计算代码如下:

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "https://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>jquery实现购物车多物品数量的加减+总价计算</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.6.1.min.js"></script>
<script>
$(function(){
$(".add").click(function(){
var t=$(this).parent().find('input[class*=text_box]');
t.val(parseint(t.val())+1)
settotal();
})
$(".min").click(function(){
var t=$(this).parent().find('input[class*=text_box]');
t.val(parseint(t.val())-1)
if(parseint(t.val())<0){
t.val(0);
}
settotal();
})
function settotal(){
var s=0;
$("#tab td").each(function(){
s+=parseint($(this).find('input[class*=text_box]').val())*parsefloat($(this).find('span[class*=price]').text());
});
$("#total").html(s.tofixed(2));
}
settotal();

})
</script>
</head>
<body>
<table id="tab">
<tr>
<td>
<span>单价:</span><span class="price">1.50</span>
<input class="min" name="" type="button" value="-" />
<input class="text_box" name="" type="text" value="1" />
<input class="add" name="" type="button" value="+" />
</td>
</tr>
<tr>
<td>
<span>单价:</span><span class="price">3.95</span>
<input class="min" name="" type="button" value="-" />
<input class="text_box" name="" type="text" value="1" />
<input class="add" name="" type="button" value="+" />
</td>
</tr>
</table>

<p>总价:<label id="total"></label></p>
</body>
</html>