<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8" />
<title>jQuery实现购物车物品数量的加减</title>
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
<script>
$(function(){
var t = $("#text_box");
$("#add").click(function(){
t.val(parseInt(t.val())+1)
setTotal();
})
$("#min").click(function(){
t.val(parseInt(t.val())-1)
if(parseInt(t.val())<0){
t.val(0);
}
setTotal();
})
function setTotal(){
//toFixed(2) 表示保留两位小数
$("#total").html((parseInt(t.val())*4).toFixed(2));
}
setTotal();
})
</script>

</head>
<body>
<p>单价:4</p>
<input id="min" name="" type="button" value="-"
style="width:20px;height:20px;margin-top:12px;background:#468CD4;text-align:center;color:#ffffff;"/>
<input id="text_box" name="" type="text" value="4" readonly= "true"
style="width:30px;height:19px; margin-top:12px; border:1px solid #ccc; text-align:center; color:#000;"/>
<input id="add" name="" type="button" value="+"
style="width:20px;height:20px; margin-top:12px; background:#468CD4; text-align:center;color:#ffffff;"/>
<p>总价:<label id="total"></label></p>
</body>
</html>