日期:2014-05-20 浏览次数:20702 次
function addProduct(img,productId) { $("#buy").css("display","none"); $("#cartInfo_"+productId).html('<img align="bottom" src="../images/load.gif" width="14" height="14" style="display:inline" align="bottom"/> 购买中...'); $.post("../cart/cart!add.action", { id: productId, time: (new Date()).getTime() }, function(json){ if(json.ok) { alert(json.ok); //没有添加成功 $("#buy").css("display","block"); $("#cartInfo_"+productId).html('<img align="bottom" src="../images/wrong.gif" width="14" height="14" style="display:inline" align="bottom"/> <span style="color:red">购买失败</span>'); } else { //添加成功 $("#cartInfo_"+productId).html('<img align="bottom" src="../images/right.gif" width="14" height="14" style="display:inline" align="bottom"/> 购买成功'); var timeId=setTimeout(function(){ clearTimeout(timeId); $("#buy").css("display","block"); $("#cartInfo_"+productId).html(""); }, 2000); } },"json" ); }
public String add() { System.out.println("id:"+id); CartDAO dao=DAOFactory.getCartDAO(); //String flag=DAOFactory.getCartDAO().add(id);//返回yes or no String flag=dao.add(id); if(flag.equals("yes")) { //如果存在,返回OK ok=true; } else { ok=false; } return "success"; }
public Map<Integer, CartItem> store = new HashMap<Integer, CartItem>(); public String add(int id) { if (store.containsKey(id)) { // 是否已经添加购买 return "yes"; } else { Product pro; try { pro = DAOFactory.getProductDAO(Constant.TYPE_BOOK).findByProId( id); CartItem cart = new CartItem(); cart.setNum(1); cart.setProduct(pro); cart.setBuy(true); store.put(id, cart); return "no"; } catch (Exception e) { e.printStackTrace(); } } return null; }