jQuery實現(xiàn)購物車多物品數(shù)量的加減+總價計算
更新時間:2014年06月06日 09:41:35 作者:
這篇文章主要介紹了jQuery實現(xiàn)購物車多物品數(shù)量的加減+總價計算,需要的朋友可以參考下
復(fù)制代碼 代碼如下:
<!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>
<title>jQuery實現(xiàn)購物車多物品數(shù)量的加減+總價計算</title>
<script type="text/javascript" src="http://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>
相關(guān)文章
jQuery是用來干什么的 jquery其實就是一個js框架
jQuery是一bai個簡潔而快速的JavaScript庫,可用于du簡化zhi事件處理,HTML文檔遍歷,Ajax交互和dao動畫,以更快速開發(fā)網(wǎng)站2021-02-02
jquery.cookie.js 操作cookie實現(xiàn)記住密碼功能的實現(xiàn)代碼
jquery.cookie.js操作cookie實現(xiàn)記住密碼功能,很簡單很強大,喜歡的朋友可以參考下。2011-04-04
jquery ajax提交表單數(shù)據(jù)的兩種方式
貌似AJAX越來越火了,作為一個WEB程序開發(fā)者要是不會這個感覺就要落伍,甚至有可能在求職的時候?qū)冶惶蕴N乙彩且粋€WEB程序開發(fā)者,當(dāng)然我也要“隨波逐流”一把,不然飯碗不保?。?2009-11-11

