jQuery实现购物车计算价格的方法


 <script language="javascript">
$(function(){
var size=3.0*$('#image1').width();
$("#image1").mouseover(function(event) {
var $target=$(event.target);
if($target.is('img'))
{
$("").css({
"height":size,
"width":size,
}).appendTo($("#imgtest"));/*将当前所有匹配元素追加到指定元素内部的末尾位置。*/
}
}).mouseout(function() {
$("#tip").remove();/*移除元素*/
})
 
})
</script>
  <script type="text/javascript">
function total(id)
{
/*计算单个的价格*/
var quantity=document.getElementById("quantity"+id).value;
var price=document.getElementById("price"+id).value;
var smallTotal=quantity*price;
var smallT=document.getElementById("smallTotal"+id);
smallT.innerHTML=smallTotal;
 
/*计算总价格*/
var totalPrice=0;
for(var a=1;a<3;a++){
var quantity=document.getElementById("quantity"+a).value;
var price=document.getElementById("price"+a).value;
var smallTotal=quantity*price;
totalPrice=totalPrice+smallTotal;
}
var total=document.getElementById("total");
total.innerHTML=totalPrice;
}
</script>
  <script type="text/javascript">
function initialize()
{
var totalPrice=0;
for(var a=1;a<3;a++){
var quantity=document.getElementById("quantity"+a).value;
var price=document.getElementById("price"+a).value;
var smallTotal=quantity*price;
totalPrice=totalPrice+smallTotal;
/*alert(smallTotal);*/
var smallT=document.getElementById("smallTotal"+a);
smallT.innerHTML=smallTotal;
}
/*取出购物车的所有商品的价格总和*/
var total=document.getElementById("total");
total.innerHTML=totalPrice;
}
</script>
  
 
 "initialize()">
  
"imgtest">


"1" style="text-align: center;" align="center"> "height: 50">
"WIDTH: 300px"> 商品名称 "WIDTH: 60px"> 图片 "WIDTH: 170px"> 数量 "WIDTH: 170px"> 价格 "WIDTH: 250px"> 小计
class="name">商品1 class="image"> "1.jpg" width="40px" height="40px" id="image1"/> class="quantity"> "quantity1" value="1" onblur="total(1);"/> class="price"> "hidden" id="price1" value="20"/> 20 class="total"> "smallTotal1">
class="name">商品2 class="image"> "1.jpg" width="40px" height="40px" id="image1"/> class="quantity"> "quantity2" value="2" onblur="total(2);"/> class="price"> "hidden" id="price2" value="30"/> 30 class="total"> "smallTotal2">
"4" class="cart_total">
class="red">总计:"total"> &nbsp;元
转载 https://www.jb51.net/article/62812.htm