jquery实现的购物车商品数量变化,价格随之变动,但是总价格怎么跟着变啊?
如图是实现价格随着商品数量变动,用jquery实现的,代码如下<td><imgsrc="../image/-.gif"align="absmiddle"onclick="...
如图是实现价格随着商品数量变动,用jquery实现的,代码如下
<td><imgsrc="../image/-.gif"align="absmiddle"onclick="jian(1)"/><input id="text_box_1"name="count"type="text"class="put1"value="1"/><imgsrc="../image/+.gif"align="absmiddle"onclick="jia(1)"/></td>
<td><spanclass="b red"id="total_1">69¥</span></td>
<tdcolspan="6"class="td2">重量总计:<span id="totalweight">100.41</span>g <span>商品总金额:<fontclass="red"id="totalmoney">¥72</font></span>元</td><inputtype="hidden"id="sumprice"value='72' >
</tr>
<script>
function jian(id){
var t = $("#text_box_"+id);
t.val(parseInt(t.val())-1)
$("#total_"+id).html((parseInt(t.val())*($("#price_"+id).val())).toFixed(2));
$("#totalweight").html((parseInt(t.val())*$("#weight_"+id).val()).toFixed(2));
}
function jia(id){
var t = $("#text_box_"+id);
t.val(parseInt(t.val())+1)
$("#total_"+id).html((parseInt(t.val())*$("#price_"+id).val()).toFixed(2));
}
</script>
怎么实现单个商品的价格随数量变动的同时,多个商品时商品的总价格也随之变动啊?
请高手指点 展开
<td><imgsrc="../image/-.gif"align="absmiddle"onclick="jian(1)"/><input id="text_box_1"name="count"type="text"class="put1"value="1"/><imgsrc="../image/+.gif"align="absmiddle"onclick="jia(1)"/></td>
<td><spanclass="b red"id="total_1">69¥</span></td>
<tdcolspan="6"class="td2">重量总计:<span id="totalweight">100.41</span>g <span>商品总金额:<fontclass="red"id="totalmoney">¥72</font></span>元</td><inputtype="hidden"id="sumprice"value='72' >
</tr>
<script>
function jian(id){
var t = $("#text_box_"+id);
t.val(parseInt(t.val())-1)
$("#total_"+id).html((parseInt(t.val())*($("#price_"+id).val())).toFixed(2));
$("#totalweight").html((parseInt(t.val())*$("#weight_"+id).val()).toFixed(2));
}
function jia(id){
var t = $("#text_box_"+id);
t.val(parseInt(t.val())+1)
$("#total_"+id).html((parseInt(t.val())*$("#price_"+id).val()).toFixed(2));
}
</script>
怎么实现单个商品的价格随数量变动的同时,多个商品时商品的总价格也随之变动啊?
请高手指点 展开
2个回答
展开全部
------------------javascript -------------------
function jia(id){
var t = $("#text_box_"+id);
var _price = parseInt($("#price_"+id).text());
var _val = parseInt(t.val())+1;
var result = (_val*_price).toFixed(2) ;
t.val(_val);
$("#total_"+id).html(result);
$("#totalmoney").html(getSum());
}
function getSum(){
var sum = 0;
$.each($("td>span[id^='total_']"),function(){
var _val = parseFloat(this.innerText);
sum += _val;
})
return sum;
}
--------------------- html 片段---------------------
<tr>
<td><span class="b red" id="price_1">69</span><span>¥</span></td>
<td><img src="../image/-.gif"align="absmiddle"onclick="jian(1)"/><input id="text_box_1" name="count" type="text" class="put1" value="1"/><img src="../image/-.gif" align="absmiddle" onclick="jia(1)"/></td>
<td><span class="b red" id="total_1">69</span><span>¥</span></td>
<td>删除</td>
</tr>
------------------------------------------------
上面是我根据你的描述,进行了适当修改后的片段~~
基本原理就是对表格中的总价列进行了累加计算,最后写到 ”商品总金额“的位置。
function jia(id){
var t = $("#text_box_"+id);
var _price = parseInt($("#price_"+id).text());
var _val = parseInt(t.val())+1;
var result = (_val*_price).toFixed(2) ;
t.val(_val);
$("#total_"+id).html(result);
$("#totalmoney").html(getSum());
}
function getSum(){
var sum = 0;
$.each($("td>span[id^='total_']"),function(){
var _val = parseFloat(this.innerText);
sum += _val;
})
return sum;
}
--------------------- html 片段---------------------
<tr>
<td><span class="b red" id="price_1">69</span><span>¥</span></td>
<td><img src="../image/-.gif"align="absmiddle"onclick="jian(1)"/><input id="text_box_1" name="count" type="text" class="put1" value="1"/><img src="../image/-.gif" align="absmiddle" onclick="jia(1)"/></td>
<td><span class="b red" id="total_1">69</span><span>¥</span></td>
<td>删除</td>
</tr>
------------------------------------------------
上面是我根据你的描述,进行了适当修改后的片段~~
基本原理就是对表格中的总价列进行了累加计算,最后写到 ”商品总金额“的位置。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询