js购物车结算代码(js淘宝购物车结算代码)
本篇文章给大家谈谈js购物车结算代码,以及js淘宝购物车结算代码对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、求助js购物车代码!要能点击 添加按钮 就直接在购物车显示出来!可以修改数量!
- 2、请问js购物车代码加在哪才能实现当顾客看到加入购物车的按钮,点击后出现弹出框呢?
- 3、很奇怪的js,ajax的问题,关于添加到购物车及点击删除的,就类似京东商城的那样。
- 4、jsp购物车代码
- 5、易买网购物车结算用jsp怎么做
求助js购物车代码!要能点击 添加按钮 就直接在购物车显示出来!可以修改数量!
script language='javascript'
function checkSum()
{
var sum = 0;
for(var i=0;idocument.form1.elements["writer"].length;i++)
{
if(document.form1.elements["writer"][i].checked)
{
sum = sum +parseInt(document.form1.elements["writer"][i].value);
}
}
totalprice.innerText = sum +" 元";
}
/script
form method=post name=form1
input type="checkbox" name=writer value="34" onclick="checkSum()"韩国耳饰tm56(34元)br
input type="checkbox" name=writer value="46" onclick="checkSum()"纯银吊坠tc34(46元)br
input type="checkbox" name=writer value="30" onclick="checkSum()"黄莺手镯ta345(30元)br
input type="checkbox" name=writer value="40" onclick="checkSum()"翡翠玉镯1346(40元)br
input type="checkbox" name=writer value="50" onclick="checkSum()"天涯耳饰1233(50元)br
br
总价为:span id="totalprice"0 元/span
/form
请问js购物车代码加在哪才能实现当顾客看到加入购物车的按钮,点击后出现弹出框呢?
加在当前页面的script的一个方法中,你点击按钮触发这个方法就行
很奇怪的js,ajax的问题,关于添加到购物车及点击删除的,就类似京东商城的那样。
初步怀疑你的删除事件绑定有问题:新加入的DOM元素未绑定到事件。这种情况应该使用事件委派来做,你用 jQuery 吗?假设你的购物车列表的 HTML 结构如下:
ul id="cartList"
li
购物车商品1
button删除/button
/li
li
购物车商品2
button删除/button
/li
……
/ul
则删除购物车商品的代码为(用了 jQuery):
$('#cartList').on('click', 'button', function() { // 委派 button 的点击事件
$(this).parent().remove(); // 移除购物车里当前商品
});
jsp购物车代码
//shopping.html
html
headtitleshopping stor/title/head
body
form action="carts.jsp" target="post"
br
please select the item that you want to buy
br
select name="item"
optionbook:old man and the sea
optionx-box game machine
optionmp3 player
optioncce
optionbook:jsp programming
optioncd "the endless love"
optiondvd "gone with the wind"
/select
br
input type="submit" name="submit" value="add"
input type="submit" name="submit" value="remove"
/form
/body
/html
------------------------------------------------------------------
//carts.jsp
%@page contentType="text/html;charset=ISO8859_1" %
html
jsp:useBean id="cart" scope="session" class="test.DummyCart"/
jsp:setProperty name="cart" property="*"/
%
cart.processRequest();
%
br
ol
you have chosen these items:
%
String []items=cart.getItems();
for(int i=0;iitems.length;i++)
{
%
li%=items[i] %/li
%
}
%
/ol
hr
%@include file="shopping.htm" %
/html
---------------------------------------------------------------------//DummyCart.java
package test;
import javax.servlet.http.*;
import java.util.Vector;
import java.util.Enumeration;
public class DummyCart
{
Vector v = new Vector();
String submit=null;
String item= null;
private void addItem(String name)
{
v.addElement(name);
}
private void removeItem(String name)
{
v.removeElement(name);
}
public void setItem(String s)
{
item=s;
}
public void setSubmit(String s)
{
submit=s;
}
public String[] getItems()
{
String []s=new String[v.size()];
v.copyInto(s);
return s;
}
public void processRequest()
{
if(submit==null)
addItem(item);
if(submit.equals("add"))
addItem(item);
else if (submit.equals("remove"))
removeItem(item);
reset();
}
private void reset()
{
submit=null;
item=null;
}
}
----------------------------------------------------------------------
上面是一个简单的例子,功能都能实现,对网页效果要求更漂亮些的可做一些修改。
易买网购物车结算用jsp怎么做
页面jsp :
%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%
%@ taglib prefix="c" uri=""%
%@ taglib uri="" prefix="fmt"%
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns=""
head
meta http-equiv="Content-Type" content="text/html; charset=utf-8" /
title易买网 - 首页/title
link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath }/css/style.css" /
script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-2.1.1.js"/script
script type="text/javascript"
var contextPath = '${pageContext.request.contextPath }';
/script
script type="text/javascript" src="${pageContext.request.contextPath }/js/shopping.js"/script
/head
body
jsp:include page="top.jsp" /
div id="position" class="wrap"
您现在的位置:a href="Home"易买网/a gt; 购物车
/div
div class="wrap"
div id="shopping"
form action="" method="post"
table
tr
th商品名称/th
th商品价格/th
th购买数量/th
th操作/th
/tr
c:forEach items="${sessionScope.shopCar}" var="item" varStatus="status"
tr id="product_id_${item.proId}"
td class="thumb"img src="${item.proImg }" height="50" width="30" /a href="Product?action=viewentityId=${item.proId}"${item.proName}/a/td
td class="price" id="price_id_1"
spanfmt:formatNumber value="${item.proPrice}" type="NUMBER" minFractionDigits="2" //span
input type="hidden" value="${item.proPrice}" /
/td
td class="number"
dl
dtspan onclick="sub('number_id_${item.proId}','${item.proId}')"-/spaninput id="number_id_${item.proId}" type="text" readonly="readonly" name="number" value="${item.proNum}" /span onclick="addNum('number_id_${item.proId}','${item.proId}')"+/span/dt
/dl
/td
td class="delete"a href="javascript:deleteItem('product_id_${item.proId}','${item.proId}')"删除/a/td
/tr
/c:forEach
/table
div class="button"input type="submit" value="" //div
/form
/div
/div
div id="footer"
Copyright copy; kaka qq 292817678 itjob 远标培训.
/div
/body
/html
页面关联的js 自己去网上下载一个jquery
/*数量减少*/
function sub(id,proId){
//购买数量的值
var num = $('#'+id).val();
if(num 1){
$('#'+id).val(num - 1);
}
edit(id,proId);
}
function edit(id,proId){
var url = contextPath + '/HomeCarManager';
//修改后的数量,购物明细的商品的id
num = $('#'+id).val();
$.post(url,{"num":num,"proId":proId},function (msg){
/*
if(msg == 'true'){
alert('修改成功');
} else {
alert('修改失败');
}*/
});
}
/**
* 数量增加
* @param {} id
*/
function addNum(id,proId){
//购买数量的值
var num = $('#'+id).val();
$('#'+id).val(parseInt(num) + 1);
edit(id,proId);
}
/**
* 删除购物明细
*/
function deleteItem(trId,proId){
//
//console.log($("#"+trId));
//js删除页面节点
//$("#"+trId).remove();
var url = contextPath + '/HomeCarManager';
$.post(url,{"proId":proId},function (msg){
if(msg == 'true'){
//js删除页面节点
$("#"+trId).remove();
}
});
}
后台servlet1
package com.kaka.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 购物车处理类
* @author @author ITJob 远标培训
*
*/
import com.kaka.entity.Items;
import com.kaka.entity.Product;
import com.kaka.service.ProductService;
import com.kaka.service.impl.ProductServiceImpl;
public class HomeCar extends HttpServlet {
private static final long serialVersionUID = 1L;
ProductService ps = new ProductServiceImpl();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取商品的id
String proId = req.getParameter("proId");
resp.setContentType("text/html;charset=UTF-8");
PrintWriter writer = resp.getWriter();
if(null != proId !"".equals(proId)){
//返回添加购物车成功
//System.out.println("=============" + proId);
//根据商品的id查询商品
try {
Integer pId = Integer.parseInt(proId);
Product product = ps.findProductById(pId);
if(null != product){
//查询到了商品,将商品的相关参数构建一个购物明细放入到购物车
Items it = new Items();
it.setProId(product.getProId());
it.setProName(product.getProName());
it.setProPrice(product.getProPrice());
it.setProImg(product.getProImg());
//先判断session范围是否有购物车
ListItems shopCar = (ListItems)req.getSession().getAttribute("shopCar");
if(null == shopCar){
//购物车
shopCar = new ArrayListItems();
}
//将商品加入到购物车之前,判断购物车中是否已经包含了该购物明细,如果包含了,只需要修改购买的数量
if(shopCar.contains(it)){
int index = shopCar.indexOf(it);//寻找购物车中包含购物明细在购物车中位置
Items items = shopCar.get(index);//获取购物车中存在的购物明细
items.setProNum(items.getProNum()+1);
} else {
shopCar.add(it);
}
//将购物车放入到session访问
req.getSession().setAttribute("shopCar", shopCar);
//返回
writer.print(true);
} else {
writer.print(false);
}
} catch (Exception e) {
e.printStackTrace();
writer.print(false);
}
} else {
writer.print(false);
}
writer.flush();
writer.close();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
后台管理servlet
package com.kaka.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.mail.FetchProfile.Item;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 购物车修改
* @author ITJob 远标培训
*
*/
import com.kaka.entity.Items;
import com.kaka.entity.Product;
import com.kaka.service.ProductService;
import com.kaka.service.impl.ProductServiceImpl;
public class HomeCarManager extends HttpServlet {
private static final long serialVersionUID = 1L;
ProductService ps = new ProductServiceImpl();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
PrintWriter writer = resp.getWriter();
//获取参数
String proId = req.getParameter("proId");
String num = req.getParameter("num");
if(null != proId null != num
!"".equals(proId) !"".equals(num)){
try {
Integer pId = Integer.parseInt(proId);
Float pNum = Float.parseFloat(num);
//根据商品的id获取对应的明细项
// 先判断session范围是否有购物车
ListItems shopCar = (ListItems) req.getSession().getAttribute("shopCar");
for(Items it : shopCar){
if(it.getProId()== pId){
it.setProNum(pNum);
}
}
writer.print(true);
} catch (Exception e) {
e.printStackTrace();
}
} else {
//删除的操作
try {
Integer pId = Integer.parseInt(proId);
//根据商品的id获取对应的明细项
// 先判断session范围是否有购物车
ListItems shopCar = (ListItems) req.getSession().getAttribute("shopCar");
Items items = null;
for(Items it : shopCar){
if(it.getProId()== pId){
items = it;
break;
}
}
if(null != items){
shopCar.remove(items);
req.getSession().setAttribute("shopCar",shopCar);
}
writer.print(true);
} catch (Exception e) {
e.printStackTrace();
}
}
writer.flush();
writer.close();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
js购物车结算代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于js淘宝购物车结算代码、js购物车结算代码的信息别忘了在本站进行查找喔。