❶ JavaScript根据这个代码,做出购物车里减一个数量的效果,跪求!我实在不会!
把
inputcount[index].value=count+1;
改为
if (count <= 0) { return };
inputcount[index].value=count--;
❷ php购物车添加和减少数量以及计算总金额
增减里来面的get的是 BID ,你传值用的自是 id ,所以session里面获取不到$I_ID的值 ,默认变成空字符串,所以你后面的增减操作都是对中间那个没有id的进行操作 .
总量遍历数组,把单价和数量相乘 然后相加 .
❸ jsp购物车修改商品数量的问题
<input type="text" id="count" value="<%=goods.getCount()%>" size="1"/>你每个商品的数量都是这个,页面出现有多个id="count" 的input域
document.getElementById("count").value;当id是count的input唯一的时候才能取到正确的
<input type="text" id="<%=goods.getProId()%>_count" value="<%=goods.getCount()%>" size="1"/>
数量的Input的id可以换一下让他变成唯一的,例如 商品id,这样就唯一了
<a href="javascript:go('<%=goods.getProId()%>')">
<img src="img/changecount.gif" title="修改商品数量" border="0" width="15" height="15" style="position: relative;top:2px;"/>
</a>
每次调用go的时候把input的商品id传过去,
function go(proId){
var str;
str="changecount.jsp?count=";
str+=document.getElementById(proId+"count").value;
str+="&&";
str+="proId=";
str+="<%=goods.getProId()%>";
window.location=str;
}
这样应该就可以了,主要是因为你那个Input 的id重复了,
❹ struts中jquery如何实现购物车中多个商品数量的增减
$("#btn").click(function(){
var txtvalue=$("商品数量input").val();
$("商品数量input").val(txtvalue+1);
});
你需要这个?还是对选择器不熟,只能选到内地一条记录?容
❺ 用html做一个购物车,能实现简单的产品数量和价格的加减就行。最后能计算出提交物品价格的总和。
html中的购物车的增减不能直接传送到后台,可以通过ajax,在js中发送ajax
纯前端的话可以参考下面的
<!DOCTYPEhtml>
<htmllang="en">
<head>
<metacharset="UTF-8">
<title>cart</title>
<styletype="text/css">
body,p,a,input{
margin:0;
padding:0;
font-size:12px;
}
.container{
width:100%;
}
.main{
width:1000px;
height:500px;
margin:100pxauto;
}
.main.cart-containertable{
width:100%;}
.main.cart-containertabletr{
text-align:center;
}
.main.cart-containertabletr:hover{
background:rgba(128,128,128,0.2);
}
.main.cart-containertable.table-header{
height:30px;
background:#d9d9d9;
font-size:1.2em;
}
.main.cart-containertable.table-headertd:first-child{
border-left:solid4pxred;
box-sizing:border-box;
}
.main.cart-containertabletrtd:nth-child(1),
.main.cart-containertabletrtd:nth-child(2){
text-align:left;
}
.main.cart-containertabletrtd:nth-child(2){
width:52%;
}
.main.cart-containertabletrtd:nth-child(3){
width:12%;
}
.main.cart-containertabletrtd:nth-child(4){
width:12%;
}
.main.cart-containertabletrtd:nth-child(5){
width:12%;
}
.main.cart-containertabletrtd:last-child{
width:10%;
}
.cart-good{
height:60px;
}
.cart-goodimg{
float:left;
margin:10px;
width:60px;
}
.cart-goodtdp{
margin:10px0px;
}
/*加、减按钮*/
.cart-goodtdinput[type='button']{
width:20px;
height:20px;
background:#00f300;
outline:none;
border:none;
}
.cart-goodtdinput[type='button']:disabled{
background:grey;
}
.cart-goodtdinput[type='button']:first-child{
margin-right:-4px;
}
.cart-goodtdinput[type='button']:last-child{
margin-left:-4px;
}
.cart-goodtdinput[type='text']{
width:30px;
height:20px;
outline:none;
border:none;
text-align:center;
}
.table-footer{
display:flex;
justify-content:space-between;
line-height:40px;
}
.table-footerdiv{
font-size:1.2em;
}
.table-footerdivbutton{
background:red;
width:120px;
height:40px;
color:white;
}
</style>
</head>
<body>
<divclass="container">
<header></header>
<sectionclass="main">
<divclass="cart-container">
<tablecellspacing="0">
<trclass="table-header">
<td><inputtype="checkbox"id="chk_alla"></td>
<td>聚美优品发货</td>
<td>聚美价</td>
<td>数量</td>
<td>小计</td>
<td>操作</td>
</tr>
<!--<trclass="cart-good">-->
<!--<td><inputtype="checkbox"id="001"></td>-->
<!--<td>-->
<!--<imgsrc="https://p2.jmstatic.com/proct/001/293/1293263_std/1293263_60_60.jpg"alt="">-->
<!--<p>[极速免税]PITTAMASK口罩3枚入</p>-->
<!--<p>型号:新版防晒款容量:3枚入</p>-->
<!--</td>-->
<!--<td>89.00</td>-->
<!--<td>-->
<!--<inputtype="button"value="-">-->
<!--<inputtype="text"value="1">-->
<!--<inputtype="button"value="+">-->
<!--</td>-->
<!--<td>89.00</td>-->
<!--<td><ahref="#">删除</a></td>-->
<!--</tr>-->
</table>
<divclass="table-footer">
<div>
<inputtype="checkbox"id="chk_allb"><labelfor="chk_allb">全选</label>
<spanstyle="margin-left:20px">继续购物|清空选中商品</span>
</div>
<div>
共<spanid="good_count">5</span>件商品商品应付总额:<spanid="goods_total">¥229.00</span>
<buttonclass="btn_menu">去结算</button>
</div>
</div>
</div>
</section>
</div>
<script>
(function(){
varskin_procts=[
{
"id":"002",
"title":"EsteeLauder多效智妍精华霜15ml",
"img_url":"http://p0.jmstatic.com/proct/003/565/3565880_std/3565880_350_350.jpg",
"price":249.0,
"number":6,
"acount":"520",
"ischecked":true
},
{
"id":"004",
"title":"EsteeLauder肌透修护洁面乳30ml",
"img_url":"http://p4.jmstatic.com/proct/003/155/3155764_std/3155764_350_350.jpg",
"price":49.9,
"number":1,
"acount":"5911",
"ischecked":false
},
{
"id":"008",
"title":"雅诗兰黛无痕持妆粉底液",
"img_url":"http://p3.jmstatic.com/proct/003/662/3662318_std/3662318_350_350.jpg",
"price":69.9,
"number":2,
"acount":"3972",
"ischecked":true
},
{
"id":"0012",
"title":"EsteeLauder肌初赋活原生液30ml",
"img_url":"http://p4.jmstatic.com/proct/003/565/3565914_std/3565914_350_350.jpg",
"price":159.0,
"number":1,
"acount":"2338"
},
{
"id":"001",
"title":"雅诗兰黛无痕持妆粉底液30ml",
"img_url":"http://p2.jmstatic.com/proct/001/648/1648502_std/1648502_350_350.jpg",
"price":298.0,
"number":4,
"acount":"5071",
"ischecked":false
},
{
"id":"009",
"title":"雅诗兰黛眼部精华霜15ml",
"img_url":"http://p1.jmstatic.com/proct/001/049/1049746_std/1049746_350_350.jpg",
"price":399.0,
"number":1,
"acount":"4022",
"ischecked":false
}
]
//添加商品
functionload(){
vartbody=document.querySelector('.cart-containertabletbody');
for(letgoodofskin_procts){
tbody.innerHTML+=`<trclass="cart-good"id="${good.id}">
<td><inputtype="checkbox"class="good-check"${good.ischecked?"checked":''}></td>
<td>
<imgsrc="${good.img_url}"alt="">
<p>[极速免税]PITTAMASK口罩3枚入</p>
<p>型号:新版防晒款容量:3枚入</p>
</td>
<td>${good.price}</td>
<td>
<inputtype="button"value="-"${good.number<=1?"disabled":''}>
<inputtype="text"value="${good.number}">
<inputtype="button"value="+">
</td>
<td>${good.price*good.number}</td>
<td><ahref="#">删除</a></td>
</tr>`
}
totalAcount();
}
load();
//endall..........
//1.为table注册单击事件
vartable01=document.querySelector('.cart-containertable');
table01.onclick=function(event){
varnode=event.target
if(node.getAttribute('type')=='button'){
//alert(event.target.value);
changeNumber(event);
subtotal(event);
checkedRow(event);
checkedAllRows();
}elseif(node.className=='good-check'){
checkedAllRows();
}elseif(node.id=='chk_alla'){
varf=event.target.checked;
varchks=document.querySelectorAll('.good-check');
for(varckofchks){
ck.checked=f;
}
for(vargoodofskin_procts){
good.ischecked=f;
}
}elseif(node.nodeName.toLowerCase()=='a'){
vartr=event.target.parentNode.parentNode;
for(vari=0;i<skin_procts.length;i++){
if(skin_procts[i].id==tr.id){
skin_procts.splice(i,1);
console.log(skin_procts);
}
}
tr.parentNode.removeChild(tr);
}
totalAcount();
};
//单击增加或减少按钮的方法
functionchangeNumber(event){
varnode=event&&event.target;
varv=0;
if(node.value&&node.value=='+'){
//node.previousElementSibling.value=parseInt(node.previousElementSibling.value)+1;
node.previousElementSibling.value++;
v=node.previousElementSibling.value;
node.previousElementSibling.previousElementSibling.disabled=false;
}else{
//if(node.value&&node.value=='+')
if(node.nextElementSibling.value>1){
node.nextElementSibling.value--;
v=node.nextElementSibling.value;
if(v==1){
node.disabled=true;
}
}
}
//存储商品数量
varid=node.parentNode.parentNode.id;
for(vargoodofskin_procts){
if(id==good.id){
good.number=v;
}
}
}
//每个商品小计的方法
functionsubtotal(event){
varnode=event&&event.target;
//varid=node.parentNode.parentNode.id;
//for(vargofskin_procts){
//if(g.id==id){
//alert(g.price);
//}
//}
//varprice=;
varprice=node.parentNode.previousElementSibling.innerText;
varnum=node.parentNode.children[1].value;
node.parentNode.nextElementSibling.innerText=(num*price).toFixed(2);
}
//检验该商品是否选中
functioncheckedRow(event){
event.target.parentNode.parentNode.firstElementChild.firstElementChild.checked=true;
//event.target.parentNode.parentNode.cells[0].firstElementChild.checked=true;
//vartbody=event.target.parentNode.parentNode.parentNode;
//event.target.parentNode.parentNode.parentNode.rows[3].cells[0].firstElementChild.checked=true;
}
//检查是否全选
functioncheckedAllRows(){
varchks=document.querySelectorAll('.good-check');
varflag=true;
for(varckofchks){
if(!ck.checked){
flag=false;
break;
}
}
document.querySelector('#chk_alla').checked=flag;
}
//统计商品总量和总价格
functiontotalAcount(){
vartotal=0;
vartotal_price=0;
varchks=document.querySelectorAll('.good-check');
for(varckofchks){
if(ck.checked){
id=ck.parentNode.parentNode.id;
for(vargoodofskin_procts){
if(id==good.id){
total+=~~good.number;
total_price=total_price+(good.number*good.price);
good.ischecked=true;
}
}
}
}
document.querySelector('#good_count').innerText=total;
document.querySelector('#goods_total').innerText=total_price;
}
})();
</script>
</body>
</html>
❻ 怎样防止刷新浏览器购物车中商品数量自动加1,php做的购物网站
用Ajax做异步提交,都是这么做的。。。addtocart();
❼ 在jsp中如何实现这个效果点击“-”数量减一,点击“+”数量加1,就像淘宝购物那样的,如下图。
这个一般用JS来实现,大致应该是这样
<!--js函数大致如下 -->
function del(){
var count = document.getElementById("count").value;
count--;
document.getElementById("count").value = count ;
if(count<=1){
document.getElementById("del0").style.display = block;
document.getElementById("del1").style.display = none;
}
if(count < total){//假定total是库存量
document.getElementById("add0").style.display = block;
document.getElementById("add1").style.display = none;
}
}
function add(){
//......和del方法类似
}
<!-- 页面处代码大致如下 -->
<img id="del1"
src="...." onclick="del()" style="dispaly:block">
<img id="del0"
src="...." style="display:none">
<input type="text" id="count" readonly> 1 </input>
<img id="add1"
src="...." onclick="add()" style="dispaly:block">
<img id="add0"
src="...." style="dispaly:none">
很久没写代码了,估计疏漏不少,不过大致是这样的思路
❽ 如何用jquery写多个购物车的数量曾减,我用jquery写了但只第一个购物车增减有用,其它的无效,代码如下:
这个时候就像前面大哥说的 不能用来表示了 可以用class
还有你的这个结构是不规范的 id一个页面只能是唯一的
为了不同时改变 其他的购物车的数量 可以来找到相对的
这个结构:
<div>
<p>单价:3.95</p>
<input class="min" name="" type="button" value="-" />
<input class="text_box" name="" type="text" value="4" />
<inputclass="add" name="" type="button" value="+" />
<p>总价:<label class="total"></label></p>
</div>
<div>
<p>单价:3.95</p>
<input class="min" name="" type="button" value="-" />
<input class="text_box" name="" type="text" value="4" />
<input class="add" name="" type="button" value="+" />
<p>总价:<label class="total"></label></p>
</div>
把每个购物车用一个div包起来
js:
$(function(){
//var t = $("#text_box");
$(".add").click(function(){
var t= $(this).parent().find(".text_box"); //根据这个来找到它相对的元素 当然也可以直接 //$(this).prev(); 这个直接来找到
t.val(parseint(t.val()+1);
setTotal($(this),t.val());
})
$(".min").click(function(){
var t= $(this).parent().find(".text_box"); //根据这个来找到它相对的元素 当然也可以直接 //$(this).next(); 这个直接来找到
t.val(parseint(t.val()-1);
setTotal($(this),t.val());
})
function setTotal(obj,number){
var total=number*$(obj).parent().find("p:first").text(); //这里算出总价
$(obj)).parent().find("total").html(total.toFixed(2));
}
// setTotal();
})
给一个建议 以后要想做好东西 布局一定要好
一个好的结构 才能让你任意操作
❾ 网页购物车的数量添加和更新怎么实现
在新增 的页面 设置一个 令牌 然后到配置文件中 加上 一个返回令牌的result