㈠ 購物車的Java代碼
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;public class ShoppingCartManager {
HashMap<String, String> hm=new HashMap<String, String>();
float totlePrice=0;
//添加book到購物車
public void addBook(String bookId,String bookQuantity){
if(hm.containsKey(bookId)){
int value=Integer.parseInt(hm.get(bookId));
value+=Integer.parseInt(bookQuantity);
hm.put(bookId, value+"");
}else{
hm.put(bookId, bookQuantity);
}
}
//修改數量
public void updateQuantity(String bookId,String bookQuantity){
hm.put(bookId, bookQuantity);
}
//獲取購物車的所有信息 並計算總價
public ArrayList<BookBean> getShoppingCart(){
ArrayList<BookBean> al=new ArrayList<BookBean>();
Iterator<String> i=hm.keySet().iterator();
String ids="";
BookTableManager btm=new BookTableManager();
while(i.hasNext()){
ids=ids+","+i.next();
}
al= btm.selectByBookIds(ids);
totlePrice=0; //清空總價,防止無限累計
for(int j=0;j<al.size();j++){
BookBean bb=al.get(j);
totlePrice+=bb.getPrice()*Integer.parseInt(getQuantityById(bb.getBookId()+""));
}
return al;
}
//獲取總價
public float getTotlePrice(){
return totlePrice;
}
//根據ID獲取數量
public String getQuantityById(String id){
String quantity=hm.get(id);
return quantity;
}
//清空購物車
public void clear(){
hm.clear();
}
//刪除購物車中的一本書
public void deleteById(String id){
hm.remove(id);
}
}
㈡ asp網站 統計購物車里商品總數 代碼(已有basket 表)
<%
Set QuantityRs = Server.CreateObject("ADODB.RecordSet")
SQL = "Select Sum(Count) as ProctCount From [basket]" '將Count數量欄位的值相加並回存答入ProdctCount變數
QuantityRs.Open SQL,Conn,1,1
Response.Write ProctCount
%>
㈢ java購物車數量修改
你是抄form表單提交的嗎?form是怎麼寫的襲,input的name做區分了嗎,沒有的話默認第一個。這種情況建議用ajax。
好吧我看了一眼你的Controller
publicObjectedit(@RequestParam(value="id")intid,@RequestParam(value="number")intnumber,HttpSessionsession,Modelmodel)
...
carService.save(car);
先不說別的,你獲取和保存都是一個,第二個怎麼可能會變。。。
㈣ 求php增加購物車數量的加減,
偽代碼:
前端:在你頁面上數量那欄增加
<div class="quantity-form"><a href="javascript:void(0);" clstag="cart_num_down" class="decrement disabled" id="decrement_8888_526830_1_1">-</a>
<input autocomplete="off" type="text" class="itxt" value="1" id="changeQuantity_8888_526830_1_1_0" minnum="1">
<a href="javascript:void(0);" clstag="cart_num_up" class="increment" id="increment_8888_526830_1_1_0">+</a>
</div>
你的購物車是個循環列表,可以去得到,每個商品的信息:id是商品
function cart_num_up(id,uid){
$.ajax({
type: "POST",
url: "CART_num.PHP",
data: {id:id, num:-1,uid:uid},
dataType: "json",
success: function(data){
if(data.status==1){
});
});
}
function cart_num_down(id,uid){
$.ajax({
type: "POST",
url: "CART_num.PHP",
data: {id:id, num:1,uid:uid},
dataType: "json",
success: function(data){
if(data.status==1){
});
});
}
點一次 加或減按鈕,觸發js向後端發起ajax請求:返回的是增加成功和失敗狀態和剩餘數量;
CART_num.PHP
$id = (int)$_POST['id'];
$num = (int)$_POST['num'];
$uid =(int)$_POST['uid']; //有封裝獲取post或get函數更好
//查庫看庫存
$sort = get_kucun_num();
//查看購物車目前數量
$cart_num = get_cart_num(uid,id);
//判斷庫存
if($sort >$cart_num){
}else{
}
if($num<0){
//減法 $sql =「update cart set cart_num = cart_num-1 where uid =uid and id =id 」
if(($cart_num-1)>1){
}else{
}){
//加法 $sql =「update cart set cart_num = cart_num+1 where uid =uid and id =id 」
}else{
//不正確的請求
}
㈤ 該如何寫購物車數量效果代碼
代碼我先不貼了,我提供下思路,具體的寫法你自己會的。分數你看回著給啦
首先得到該產品答的數量最大值
獲取文本框的onchange事件,在此事件中,比較文本值和最大值,如果小於最大值,就不做任何操作,如果大於最大值,就彈出對話框,已經超過了最大數量,或者在頁面上設置一個顯示提示信息的文本框。
㈥ 請問,你知道帝國cm會員空間模板調用購物車購物數量的代碼嗎
根據購物車COOKIE的格式,我們得到欄目ID,內容ID,購買數量(不一定是必須的),之後組合格式,刪除COOKIE中的此段內容,重新打開購物車/或刷新購物車頁面,這樣就可以不用調整購物車,二直接刪除某件商品;
要點:
1.需要一個PHP腳本頁從用戶刪除商城COOKIE中刪除提交的商品
2.為了直觀方便體驗好,還是使用AJAX效果(trylife使用的是JQuery),所以又下面2條
3.給購物車商品行加個ID格式為:proct-欄目ID-信息ID-購買數量
3.商品行每行增加一個刪除按鈕
3.一個ajax函數用於處理刪除動作
文件修改與增加:
修改文件:e/template/ShopSys/buycar.php 商城購物車模板
增加問津:e/trylife/ShopSys-demo/del-proct.php 刪除單個商品處理文件
購物車cookie聲明與讀取
聲明: SetBuycar($buycar) e/class/ShopSysFun.php
讀取: $buycar=getcvar(『mybuycar』);
帝國CMScookie操作函數查看《esetcookie() getcvar() -帝國CMS cookie 操作函數》
這樣做下來就是做這個購物車的代碼。
㈦ ASP如何更改購物車中商品數量和計算總價
這是因為當購物車有兩種或以上的商品時,你的actionid的值就會變成形如"12, 22, 25"的形式,成了字元串,而不是數值型,所以會出錯,
我幫你修改了一下,就是把那個form表單位置改改就行了,改成每個商品都在自己的一個form表單里,這樣就不會出錯了!!
<%
Set conn = Server.CreateObject("ADODB.Connection")
conn.open mallDSN
set rs=server.createobject("ADODB.Recordset")
rs.open "select actionid,id,cpsl,yunfei,proctnum,goods,style,state,paid from orders1 where username='"&username&"' and yunfei=0 ",conn,1,3
do while not rs.eof%>
<form name="form" method="post" action="add.asp?action=cpsl&actionid=<%=rs("actionid")%>">
<tr bgcolor="#FFFFFF">
<td width="7%" height="89" align="center" style='PADDING-LEFT: 5px'><input name="id" type="checkbox" checked="checked" value="<% = rs("id") %>" />
</td>
<td align="left" style='PADDING-LEFT: 5px' width="16%"><div align="center"><img src="<% = rs("goods")%>" width="75" height="75" border="0" /></div></td>
<td align="center" width="13%"><a class="a5" href="proct.asp?id=<% = rs("id") %>" target="_blank">
<% = rs("proctnum") %>
</a></td>
<td align="center" width="13%"><font color="#FF0000"> <%=rs("paid")%></font> 元</td>
<td align="center" width="13%"><font color="#FF0000">
<input name="cpsl" type="text" id="cpsl" style="font-size:12px" value="<% = rs("cpsl")%>" size=3 maxlength=20>
</font></td>
<td align="center" width="13%">
<input type="submit" name="Submit4" value="修改"></td>
<td align="center">
<%
response.Write "<a href=add.asp?action=del&actionid="&rs("actionid")&">"
response.Write "<img src=images/trash.gif border=0></a></td></tr></form>"
rs.movenext
loop
rs.close
set rs=nothing
response.write "<tr><td height=36 colspan=6 bgcolor=#FFFFFF ><div align=center> "
if action<>"addtocart" then
%>
<input name="Submit22" type="button" onclick="MM_goToURL('parent','proct.asp');return document.MM_returnValue" value="繼續購物" />
<input name="Submit23" type="button" value="去收銀台" onclick="this.form.action='zxoderok.asp';this.form.submit()" />
<%
end if
%>
</td>
</tr>
</table>
試試看,還有問題就給我在網路里留言!
㈧ 無刷新修改購物車產品數量得總金額的js代碼
用你給的代碼基礎上用了一個最簡單的js計算,數量是鍵盤輸入,也可以在左右兩側加上自增減的操作,望採納!
<table width="500" border="0" cellpadding="5" cellspacing="0">
<tr><td>數量</td><td>單價</td></tr>
<tr>
<td><input type="text" id="num" value="1" size="5" onchange="setTotal();"/></td>
<td><span id="fare">6.00</span>元</td>
</tr>
<tr>
<td> </td>
<td>總金額:¥:<span id="total_Price">6</span>.00</td>
</tr>
</table>
<p>數量那裡最好能鍵盤輸入,不然買100件就有的點了。麻煩再給看看哈</p>
<script type="text/JavaScript">
function setTotal(){
var fare=document.getElementById("fare").innerHTML;
//alert(fare);
var num=document.getElementById("num").value;
if(/\D/.test(num)){//檢查用戶輸入的值是否是數字
alert("請您輸入正確的數量!");
document.getElementById("total_Price").innerHTML="0"
}else{
document.getElementById("total_Price").innerHTML=parseFloat(fare*num);
}
}
</script>
㈨ 如何用jquery寫多個購物車的數量曾減,我用jquery寫了但只第一個購物車增減有用,其它的無效,代碼如下:
這個時候就像前面大哥說的 不能用來表示了 可以用class
還有你的這個結構是不規范的 id一個頁面只能是唯一的
為了不同時改變 其他的購物車的數量 可以來找到相對的
這個結構:
<div>
<p>單價:3.95</p>
<input class="min" name="" type="button" value="-" />
<input class="text_box" name="" type="text" value="4" />
<inputclass="add" name="" type="button" value="+" />
<p>總價:<label class="total"></label></p>
</div>
<div>
<p>單價:3.95</p>
<input class="min" name="" type="button" value="-" />
<input class="text_box" name="" type="text" value="4" />
<input class="add" name="" type="button" value="+" />
<p>總價:<label class="total"></label></p>
</div>
把每個購物車用一個div包起來
js:
$(function(){
//var t = $("#text_box");
$(".add").click(function(){
var t= $(this).parent().find(".text_box"); //根據這個來找到它相對的元素 當然也可以直接 //$(this).prev(); 這個直接來找到
t.val(parseint(t.val()+1);
setTotal($(this),t.val());
})
$(".min").click(function(){
var t= $(this).parent().find(".text_box"); //根據這個來找到它相對的元素 當然也可以直接 //$(this).next(); 這個直接來找到
t.val(parseint(t.val()-1);
setTotal($(this),t.val());
})
function setTotal(obj,number){
var total=number*$(obj).parent().find("p:first").text(); //這里算出總價
$(obj)).parent().find("total").html(total.toFixed(2));
}
// setTotal();
})
給一個建議 以後要想做好東西 布局一定要好
一個好的結構 才能讓你任意操作
㈩ 急,寫一個購物車的代碼
我好像有一個類似的,購物車,回去給你找找,報個QQ,