① jquery实现购物车物品加减 没效果,求解
你选择器来取错了,没取到两个按钮:自
注意看,#是 id选择器,class选择器是以“.”开头的。
你的代码里面 加减两个按钮的 id分别为:add1 和 min1 而它们的class为: add和min
所以正确的做法是
$("#add") ---> $("#add1") 、 $("#min") ---> $("#min1")
或者
$("#add") ---> $(".add") 、 $("#min") ---> $(".min")
有不明白的欢迎追问^_^
② jquery 实现加入购物车功能
参考以下代码:
注意需要导入.js.
<!DOCTYPEhtml>
<html>
<head>
<title>购物车----jQuery</title>
<metacharset="utf-8"/>
<styletype="text/css">
h1{
text-align:center;
}
table{
margin:0auto;
width:60%;
border:2pxsolid#aaa;
border-collapse:collapse;
}
tableth,tabletd{
border:2pxsolid#aaa;
padding:5px;
}
th{
background-color:#eee;
}
</style>
<scripttype="text/javascript"src="./js/jquery.js"></script>
<scripttype="text/javascript">
functionadd_shoppingcart(btn){//将btn(dom)转换为jQuery对象
//先获取商品名字和单价还有库存以备后面使用
var$tds=$(btn).parent().siblings();
//$tds.eq(0)是jQuery对象$tds[0]是DOM对象
varname=$tds.eq(0).html();//string
varprice=$tds.eq(1).html();//string
varstock=$tds.eq(3).html();//string
//查看库存是否还有<=0
if(stock<=0){
return;
}
//无论购物车中是否有该商品,库存都要-1
$tds.eq(3).html(--stock);
//在添加之前确定该商品在购物车中是否存在,若存在,则数量+1,若不存在则创建行
var$trs=$("#goods>tr");
for(vari=0;i<$trs.length;i++){
var$gtds=$trs.eq(i).children();
vargName=$gtds.eq(0).html();
if(name==gName){//若存在
varnum=parseInt($gtds.eq(2).children().eq(1).val());
$gtds.eq(2).children().eq(1).val(++num);//数量+1
//金额从新计算
$gtds.eq(3).html(price*num);
return;//后面代码不再执行
}
}
//若不存在,创建后追加
varli=
"<tr>"+
"<td>"+name+"</td>"+
"<td>"+price+"</td>"+
"<tdalign='center'>"+
"<inputtype='button'value='-'onclick='decrease(this);'/>"+
"<inputtype='text'size='3'readonlyvalue='1'/>"+
"<inputtype='button'value='+'onclick='increase(this);'/>"+
"</td>"+
"<td>"+price+"</td>"+
"<tdalign='center'>"+
"<inputtype='button'value='x'onclick='del(this);'/>"+
"</td>"+
"</tr>";
//追加到#goods后面
$("#goods").append($(li));
//总计功能
total();
}
//辅助方法--单击购物车中的"+""-""x"按钮是找到相关商品所在td,以jQuery对象返回
functionfindStock(btn){
varname=$(btn).parent().siblings().eq(0).html();//获取商品名字
//注意table默认有行分组,若此处使用$("#table1>tr:gt(0)")则找不到任何tr
var$trs=$("#table1>tbody>tr:gt(0)");
for(vari=0;i<$trs.length;i++){
varfName=$trs.eq(i).children().eq(0).html();
if(name==fName){//找到匹配的商品
return$trs.eq(i).children().eq(3);
}
}
}
//增加"+"功能
functionincrease(btn){
//获取该商品库存看是否<=0
var$stock=findStock(btn);
varstock=$stock.html();
if(stock<=0){
return;
}
//库存-1
$stock.html(--stock);
//购物车数据改变
var$td=$(btn).prev();
varnum=parseInt($td.val());//number
//num此时为number类型(在计算时会自动转换为number类型)
$td.val(++num);
//获取单价,再加计算前要先转换为number类型
varprice=parseInt($(btn).parent().prev().html());
$(btn).parent().next().html(num*price);
//总计功能
total();
}
//减少"-"功能
functiondecrease(btn){
//该商品数量=1时候不能再减少
varnum=parseInt($(btn).next().val());
if(num<=1){
return;
}
var$stock=findStock(btn);
//库存+1
varstock=$stock.html();
$stock.html(++stock);
//商品数量-1
$(btn).next().val(--num);
//从新计算金额
varprice=parseInt($(btn).parent().prev().html());
$(btn).parent().next().html(price*num);
//总计功能
total();
}
//"x"删除按钮功能
functiondel(btn){
//将商品数量归还库存
var$stock=findStock(btn);
varstock=parseInt($stock.html());
varnum=parseInt($(btn).parent().prev().prev().children().eq(1).val());
$stock.html(num+stock);
//清空改行商品列表
$(btn).parent().parent().remove();
//总计功能
total();
}
//总计功能
functiontotal(){
//获取所有购物车中的trs
var$trs=$("#goodstr");
varamount=0;
for(vari=0;i<$trs.length;i++){
varmoney=parseInt($trs.eq(i).children().eq(3).html());
amount+=money;
}
//写入总计栏
$("#total").html(amount);
}
</script>
</head>
<body>
<h1>真划算</h1>
<tableid="table1">
<tr>
<th>商品</th>
<th>单价(元)</th>
<th>颜色</th>
<th>库存</th>
<th>好评率</th>
<th>操作</th>
</tr>
<tr>
<td>罗技M185鼠标</td>
<td>80</td>
<td>黑色</td>
<td>5</td>
<td>98%</td>
<tdalign="center">
<inputtype="button"value="加入购物车"onclick="add_shoppingcart(this);"/>
</td>
</tr>
<tr>
<td>微软X470键盘</td>
<td>150</td>
<td>黑色</td>
<td>9028</td>
<td>96%</td>
<tdalign="center">
<inputtype="button"value="加入购物车"onclick="add_shoppingcart(this);"/>
</td>
</tr>
<tr>
<td>洛克iphone6手机壳</td>
<td>60</td>
<td>透明</td>
<td>672</td>
<td>99%</td>
<tdalign="center">
<inputtype="button"value="加入购物车"onclick="add_shoppingcart(this);"/>
</td>
</tr>
<tr>
<td>蓝牙耳机</td>
<td>100</td>
<td>蓝色</td>
<td>8937</td>
<td>95%</td>
<tdalign="center">
<inputtype="button"value="加入购物车"onclick="add_shoppingcart(this);"/>
</td>
</tr>
<tr>
<td>金士顿U盘</td>
<td>70</td>
<td>红色</td>
<td>482</td>
<td>100%</td>
<tdalign="center">
<inputtype="button"value="加入购物车"onclick="add_shoppingcart(this);"/>
</td>
</tr>
</table>
<h1>购物车</h1>
<table>
<thead>
<tr>
<th>商品</th>
<th>单价(元)</th>
<th>数量</th>
<th>金额(元)</th>
<th>删除</th>
</tr>
</thead>
<tbodyid="goods">
</tbody>
<tfoot>
<tr>
<tdcolspan="3"align="right">总计</td>
<tdid="total"></td>
<td></td>
</tr>
</tfoot>
</table>
</body>
</html>
最终效果图:
③ 加入购物车 jquery怎么实现
两种办法,一种是存到cookie,一种是存到数据库。淘宝的购物车,是存到数据库的。
④ jquery 弹出框,如下图 ,加入购物车效果
<!DOCTYPEhtml>
<htmllang="en"xmlns="http://www.w3.org/1999/xhtml">
<head>
<metacharset="utf-8"/>
<title></title>
<scripttype="text/javascript"src="jquery.min.js"></script>
<styletype="text/css">
.des{
width:400px;
margin:0auto;
}
.add2carttip{
width:220px;
height:90px;
padding:0;
border:1pxsolidrgba(69,66,66,0.91);
background-color:#393939;
margin:0auto;
border-radius:12px12px;
position:fixed;
display:none;
}
.add2carttip>div{
vertical-align:middle;
color:#D0D0D0;
text-align:center;
font-size:1.2em;
font-weight:bold;
margin:0;
}
.c-title{
height:39px;
line-height:39px;
border-bottom:1px#D0D0D0solid;
}
.c-content{
height:49px;
line-height:49px;
border-top:1px#D0D0D0solid;
}
.c-add,.c-sub{
display:inline-block;
vertical-align:middle;
width:20px;
font-size:1.5em;
font-weight:bold;
cursor:pointer;
}
.c-sub{
vertical-align:-5px;
font-size:1.8em;
}
.c-count{
width:30px;
height:30px;
border-radius:15px;
border:none;
text-align:center;
font-size:inherit;
}
.c-action{
display:inline-block;
cursor:pointer;
}
.c-add:hover,.c-sub:hover,.c-action:hover{
color:#FAFAFA;
}
</style>
<scripttype="text/javascript">
$(function(){
var$tip=$('.add2carttip');
var$add=$('.c-add');
var$sub=$('.c-sub');
var$count=$('.c-count');
var$action=$('.c-action');
var$win=$(window);
functionsetTipPosition(){
$tip.animate({
left:(($win.width()-$tip.width())/2)+'px',
top:($win.height()*1/3)+'px'
},500);
}
setTipPosition();
$win.on('resize',setTipPosition);
$tip.on('selectstart',function(e){e.preventDefault();returnfalse;});
$add.on('click',function(){
$count.val(parseInt($count.val())+1);
});
$sub.on('click',function(){
$count.val(parseInt(($count.val())-1)||1);
});
$action.on('click',function(){
alert('add'+$count.val()+'tocart.');
});
$('.toggleTip').on('click',function(){
$tip.fadeToggle(500);
});
});
</script>
</head>
<body>
<divclass="des">
不支持IE9以下的IE浏览器
<ahref="javascript:void();"class="toggleTip">显示或隐藏</a>
</div>
<divclass="add2carttip">
<divclass="c-title">
商品详情
</div>
<divclass="c-content">
<spanclass="c-add">+</span>
<inputtype="text"class="c-count"value="1"/>
<spanclass="c-sub">-</span>
<spanclass="c-action">
加入购物车
</span>
</div>
</div>
</body>
</html>
代码 。。。
⑤ 如何用jquery实现购物车加减
直接删除就是了。不过现在都在赛客宝贝街购物。正品,选择多。
⑥ 关于jQuery,想要实现一个类似于购物车的功能。但是在将商品添加了以后,无法在购物车中删除,请问和解
因为second中初始是没有li的,所以点击事件没注册上去,应改为:
$("#second li").live("click", function() {
$(this).remove();
});
⑦ 怎样把 jquery仿淘宝规格颜色选择的产品添加到购物车里》
CGPoint point = containerView.contentOffset;
CGFloat zy = point.y ;
CGFloat py = rect.origin.y + rect.size.height;
if (py - zy <0) {
return FALSE;
}
CGRect screenRect = containerView.frame;
CGFloat by = screenRect.size.height + zy ;
if (rect.origin.y > by) {
return FALSE;
}
return TRUE;
⑧ 有没有添加购物车的代码 JavaScript 或者jquery 的都可以
添加购物车需要和服务器端进行数据交互,前端主要是用js的ajax功能将产品的id或者其他信息提交给服务器端,然后等服务器端返回添加成功,再在页面上的购物车中增加添加的商品。
⑨ jquery问题购物车加减按钮
因为一个页面中只能存在一个 id 为 add 的元素,根据你现在的代码,如果有10行记录,那就会有10个 id 为 add 的 input。
所以你要把这些 input 的 id 都改为 class,text_box 的值也要根据每次点击来判断
<input class="min" name="" type="button" value="-" />
<input class="text_box" name="goodnum" type="text" value="${item.value.quantity }" style="width:25px;" />
<input class="add" name="" type="button" value="+" />
$(".add").click(function() {
// $(this).prev() 就是当前元素的前一个元素,即 text_box
$(this).prev().val(parseInt($(this).prev().val()) + 1);
setTotal();
});
$(".min").click(function() {
// $(this).next() 就是当前元素的下一个元素,即 text_box
$(this).next().val(parseInt($(this).next().val()) - 1);
setTotal();
});
⑩ jquery实现添加到购物车拖放功能 像百度旅游定制旅游一样 如下图
这功能网络内部有专门开发的。
拖拉带拽的功能你可以看下jquery ui,里面有部分功能可以拖动和获取的,这功能要实现,代码会有点复杂。
你先自己搜搜看吧