代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
script>
购物车 $(function() {
//点击加号购物车数量增加1
$(".add").live("click",function(){
var isAdd = true;
var num = parseInt($(this).closest("li").find("#num").text());
var productId = $(this).closest("li").find("#productId").val();
var totalprice = parseFloat($("#totalprice").text());
var price = parseFloat($(this).closest("ul").find("#price").text());
if (!isNaN(num)) {
num++;
if (num > 99) {
num = 99;
isAdd = false;
}
if(isAdd) {
totalprice = totalprice + price;
}
}
var total = num * price;
$(this).closest("li").find("#num").text(num);
$(this).closest("ul").find("#total").text(total);
$.post("<%=basePath%>updateShopCart.action","productId="+productId+"&num="+num,function(data){
if(data.success==true){
$("#totalprice").text(totalprice);
}
});
});
//点击加号购物车数量减少1
$(".delete").live("click",function(){
var isReduce = true;
var num = parseInt($(this).closest("li").find("#num").text());
var productId = $(this).closest("li").find("#productId").val();
var totalprice = parseFloat($("#totalprice").text());
var price = parseFloat($(this).closest("ul").find("#price").text());
if (!isNaN(num)) {
num--;
if (num < 1) {
num = 1;
isReduce = false;
}
if(isReduce){
totalprice = totalprice - price;
}
}
var total = num * price;
$(this).closest("li").find("#num").text(num);
$(this).closest("ul").find("#total").text(total);
$.post("<%=basePath%>updateShopCart.action","productId="+productId+"&num="+num,function(data){
if(data.success==true){
$("#totalprice").text(totalprice);
}
});
});
$("#cha").live("click",function() {
var productId = $(this).closest("ul").find("#productId").val();
$.post("<%=basePath%>deleteFromShopCart.action","productId="+productId,function(data){
if(data.success==true){
getShopCartInfo();
}
});
});
function getShopCartInfo(){
$.ajax({
type : "POST",
url : "<%=basePath%>findShopCart.action",
success : function(data) {
var row = "";
var list = data.list;
$("#main_t_l").empty();
var total = 0;
if(list!=null&&list.length!=0){
for(var i=0;i
total = total + list[i].price * list[i].num;
row = ""+
""+list[i].productname+""+
""+list[i].price+"¥"+
""+
""+
""+
""+
""+
""+
""+list[i].totalprice+"¥"+
""+
"
image/cha.png\" style=\"width: 25px;height: 25px\" id=\"cha\">"+
""+
""+
"
";
$(row).appendTo($("#main_t_l"));
$("#totalprice").find("span").text(total);
}
}else {
$("#main_t_l").append("您的购物车为空,快去购物吧!");
$("#totalprice").find("span").text(0);
}
}
});
}
//提交订单
$("#ji").click(function(){
var name = $("#name").val();
var tele = $("#tele").val();
var address = $("#address").val();
var totalprice = $("#totalprice").find("span").text();
if(totalprice == 0){
alert("购物车为空,不能提交订单!");
return;
}
if(name==""){
alert("姓名不能为空!");
return;
}
if(tele==""){
alert("电话不能为空!");
return;
}
if(address==""){
alert("地址不能为空!");
return;
}
$("#ji").attr("disabled","disabled");
$("#ji").css("background","#808080");
document.getElementById("bgDiv").style.visibility = "visibility";
document.getElementById("myspin").style.visibility = "visibility";
$.post("<%=basePath%>addShopCart.action","name="+name+"&telephone="+tele+"&address="+address,function(data){
if(data.success == true) {
getShopCartInfo();
alert("提交成功");
}else {
alert("提交失败,请重新提交!");
$("#ji").addClass("ji");
$("#ji").attr("disabled","");
}
});
});
window.onload = getShopCartInfo();
window.onload = function() {
document.getElementById("bgDiv").style.visibility = "hidden";
document.getElementById("myspin").style.visibility = "hidden";
};
});
//验证联系方式
function validTeleNum() {
var tel = /(^[0-9]{3,4}[0-9]{7,8}$)|(^[0-9]{7,8}$)|(^[0-9]{3,4}\-[0-9]{7,8}$)|(^[0-9]{7,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/;
if (document.getElementById("tele").value != '') {
if (!tel.test(document.getElementById("tele").value)) {
alert("联系电话格式不正确,请重新输入!");
document.getElementById("tele").value = "";
}
}
}
script>
单品
价格
数量
合计
删除
总计
>