购物车的简单实现

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<h2 style="text-align:left">商品列表</h2>
<table width="800" border="0" id="tblProduct" cellpadding="1" cellspacing="1" bgcolor="black"> 
  <tr id="trMaster" bgcolor="white" style="display:none">
    <td>
    编号:<span>000001</span><br />
    名称:<span>aa</span><br />
    价格:<span>2.5</span><br />
    描述:<span>aaaaa</span><br />
   <div style="text-align:right"><input type="button" name="btnBuy" value="buy" /></div>
    </td>
    <td>  
    编号:<span>000001</span><br />
    名称:<span>aa</span><br />
    价格:<span>2.5</span><br />
    描述:<span>aaaaa</span><br />
   <div style="text-align:right"><input type="button" name="btnBuy" value="buy" /></div>  
    </td>
     <td> 
    编号:<span>000001</span><br /> 
    名称:<span>aa</span><br />
    价格:<span>2.5</span><br />
    描述:<span>aaaaa</span><br />
   <div style="text-align:right"><input type="button" name="btnBuy" value="buy" /></div>  
    </td>
    <td> 
    编号:<span>000001</span><br /> 
    名称:<span>aa</span><br />
    价格:<span>2.5</span><br />
    描述:<span>aaaaa</span><br />   
   <div style="text-align:right"><input type="button" name="btnBuy" value="buy" /></div>  
    </td>    
  </tr>
</table>
<h2 style="text-align:left">购物车</h2>
<table width="800" border="0" id="tblOrder" cellpadding="1" cellspacing="1" bgcolor="black">
 <tr bgcolor="white"><td>序号</td><td>编号</td><td>名称</td><td>描述</td><td>数量</td><td>单价</td><td>总价</td><td>删除</td></tr>
    <tr id="orderMarter" bgcolor="white" style="display:none">
    <td style=" width:5%">1</td>
    <td style=" width:10%">000001</td>
    <td style=" width:10%">aa</td>
    <td>aaaaa</td>
    <td style=" width:15%">
    <input type="text" name="txtNum" style="width:50px;" value="0" />
    <input type="button" name='btnAdd' style="width:10px; text-align:center" value="+" />
    <input type="button" name='btnCut' style="width:10px; text-align:center" value="-" />
    </td>
    <td style=" width:10%">0</td>
    <td style=" width:10%"><font color="red">0</font></td>
    <td style=" width:5%; text-align:center"><input type="button" name='btnRemove' style="width:30px; text-align:center" value="X" /></td>
    </tr>
</table>
<table width="800" border="0" id="tblTotal" cellpadding="1" cellspacing="1" bgcolor="black">
 <tr bgcolor="white" align="right">
    <td>总价</td>
    <td id="tdTotal"><font color="#FF0000" size="+1" face="Arial, Helvetica, sans-serif">0.0</font></td>
    </tr>
</table>

</body>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
var product=[
{prodId:"000001",prodName:"aa",price:2.5,description:"aaaaa"},
{prodId:"000002",prodName:"bb",price:2.4,description:"bbbbb"},
{prodId:"000003",prodName:"cc",price:2.3,description:"ccccc"},
{prodId:"000004",prodName:"dd",price:2.2,description:"ddddd"},
{prodId:"000005",prodName:"ee",price:2.1,description:"eeeee"},
{prodId:"000006",prodName:"ff",price:2.0,description:"fffff"},
{prodId:"000007",prodName:"gg",price:1.9,description:"ggggg"},
{prodId:"000008",prodName:"hh",price:1.8,description:"hhhhh"},
{prodId:"000009",prodName:"ii",price:1.7,description:"iiiii"}
];
</script>


<script type="text/javascript">
var j=0,i=-1,k=-1,len=$("#trMaster td").size();//数据和模板的td 都可以自定义,格式正确即可
$.each(product,function(index,prod){
 i++;
 k++;
 if(i%len==0){
  j++;
   CloneTR(j);//根据模板来克隆行
 }
 if(k==len)
  k=0;
 setTD(j,k,prod.prodId,prod.prodName,prod.price,prod.description);//给克隆行的td 赋值
});
//根据模板来克隆行
function CloneTR(id){
 $("#trMaster").clone(true).css("display","block").attr("id","tr"+id).appendTo("#tblProduct");
 $("#tr"+id+" td span").empty();
}
//给克隆行的td 赋值
function setTD(trId,index,prodId,prodName,price,description){
 var tr=$("#tr"+trId);   
 $(tr).find("td:eq("+index+")").find("span:eq(0)").html(prodId);
 $(tr).find("td:eq("+index+")").find("span:eq(1)").html(prodName);
 $(tr).find("td:eq("+index+")").find("span:eq(2)").html(price); 
 $(tr).find("td:eq("+index+")").find("span:eq(3)").html(description); 
}
var tempId="";
var num=0;
//点击buy
$("input[name='btnBuy']").click(function(){
var elem=$(this).parent().parent();//获取点击的button 的td
 var prodId=$(elem).find("span:eq(0)").html();
 var prodName=$(elem).find("span:eq(1)").html();
 var price=$(elem).find("span:eq(2)").html();
 var description=$(elem).find("span:eq(3)").html();
 if(prodId==""||prodId==null||prodId==undefined){
  alert("请点击其他产品");
 }else{  
  if(tempId.indexOf(prodId)!=-1){
   if(confirm('已存在,确定数量+1 吗?')){
    $("#tblOrder tr:gt(1)").each(function(){
     if($(this).find("td:eq(1)").html()==prodId){
      var num=$(this).find("td:eq(4)").find("input[name='txtNum']").attr("value");     
      $(this).find("td:eq(4)").find("input[name='txtNum']").attr("value",+num+1);//相当于parseInt(num)+1;
     }
    });
   }
  }else{
   num++;
   CloneOrder(num,prodId,prodName,price,description);
  }
  tempId+=prodId+","; 
  $("#tdTotal").html("<font color='#FF0000' size='+1' face='Arial, Helvetica, sans-serif'>"+GetTotalPrice().toFixed(2)+"</font>");
 }
});
//根据订单模板来clone 订单
function CloneOrder(id,prodId,prodName,price,description){
 $("#orderMarter").clone(true).css("display","block").attr("id","tro"+id).appendTo("#tblOrder");
 var tr=$("#tro"+id); 
 $(tr).find("td:eq(0)").html(num);
 $(tr).find("td:eq(1)").html(prodId);
 $(tr).find("td:eq(2)").html(prodName);
 $(tr).find("td:eq(3)").html(description); 
 $(tr).find("td:eq(4)").find("input[name='txtNum']").attr("value","1");
 $(tr).find("td:eq(5)").html(price);
 $(tr).find("td:eq(6)").html("<font color='red'>"+price+"</font>");
}
//获取总共价格
function GetTotalPrice(){
 var totalNum=0;
 $("#tblOrder tr:gt(1)").each(function(){
   var value=parseFloat($(this).find("td:eq(6)").text());   
   totalNum+=value;
 });
 return totalNum;
}
$(function(){
 $("#tblOrder input[name='txtNum']").bind("propertychange",function(){
  var value=$(this).val();
  var tr=$(this).parent().parent();
  if(value==0){
   if(confirm('确定要删除该商品吗?')){
    $(tr).remove();
   }
  }else{
   var price=$(tr).find("td:eq(5)").html();
   var total=value*price;
   $(tr).find("td:eq(6)").html("<font color='red'>"+total.toFixed(2)+"</font>");  
   $("#tdTotal").html("<font color='#FF0000' size='+1' face='Arial, Helvetica, sans-serif'>"+GetTotalPrice().toFixed(2)+"</font>");
  }
 });
 //加1
 $("#tblOrder input[name='btnAdd']").click(function(){
  var txtBox=$(this).parent().parent().find("td:eq(4)").find("input[name='txtNum']");
  var value=$(txtBox).attr("value");
   value=+value+1;
   $(txtBox).attr("value",value)
  
 });
 //减1
 $("#tblOrder input[name='btnCut']").click(function(){
  var txtBox=$(this).parent().parent().find("td:eq(4)").find("input[name='txtNum']");  
  var tr=$(this).parent().parent();
  var value=$(txtBox).attr("value");
  if(value>1){
   value=+value-1;
   $(txtBox).attr("value",value)
  }else{
   if(confirm('确定要删除该商品吗?')){
    $(tr).remove();
    $("#tdTotal").html("<font color='#FF0000' size='+1' face='Arial, Helvetica, sans-serif'>"+GetTotalPrice().toFixed(2)+"</font>");
   }
  }
 });
 
 //删除btnRemove
 $("#tblOrder input[name='btnRemove']").click(function(){
  var tr=$(this).parent().parent();
  if(confirm('确定要删除该商品吗?')){
    $(tr).remove();
    $("#tdTotal").html("<font color='#FF0000' size='+1' face='Arial, Helvetica, sans-serif'>"+GetTotalPrice().toFixed(2)+"</font>");
  }
 });
});

</script>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java购物车简单实现可以通过以下步骤来完成: 1. 创建商品类(Product):定义商品的属性,如商品名称、价格等,并提供相应的构造方法和获取属性值的方法。 2. 创建购物车类(ShoppingCart):购物车类用于管理用户选择的商品。可以使用一个列表或者数组来存储用户选择的商品。购物车类应该提供以下功能: - 添加商品到购物车 - 从购物车中移除商品 - 获取购物车中的所有商品 - 计算购物车中商品的总价 3. 创建主程序类(Main):在主程序类中,可以创建商品对象和购物车对象,并通过调用购物车类的方法来实现购物车的功能。 下面是一个简单的Java购物车实现的示例代码: ```java // 商品类 class Product { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public String getName() { return name; } public double getPrice() { return price; } } // 购物车类 class ShoppingCart { private List<Product> products; public ShoppingCart() { products = new ArrayList<>(); } public void addProduct(Product product) { products.add(product); } public void removeProduct(Product product) { products.remove(product); } public List<Product> getProducts() { return products; } public double getTotalPrice() { double totalPrice = 0; for (Product product : products) { totalPrice += product.getPrice(); } return totalPrice; } } // 主程序类 public class Main { public static void main(String[] args) { // 创建商品对象 Product product1 = new Product("商品1", 10.0); Product product2 = new Product("商品2", 20.0); // 创建购物车对象 ShoppingCart shoppingCart = new ShoppingCart(); // 添加商品到购物车 shoppingCart.addProduct(product1); shoppingCart.addProduct(product2); // 获取购物车中的所有商品 List<Product> products = shoppingCart.getProducts(); for (Product product : products) { System.out.println("商品名称:" + product.getName() + ",价格:" + product.getPrice()); } // 计算购物车中商品的总价 double totalPrice = shoppingCart.getTotalPrice(); System.out.println("购物车中商品的总价:" + totalPrice); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值