单击多次时,购物产品的数量不会刷新
当我单击产品时,我希望该数量增加1个功能正在工作,但在购物车中没有显示,除非我添加当时购物车中不存在的另一个产品。有帮助吗?
我尝试过,但找不到错误。我认为错误是在“ if(存在){...}”中,但不知道在哪里。
我对其进行了编辑以添加更多代码
const addToCart = document.querySelectorAll('.addToCart');
let carritoEl = document.querySelector('.ul');
let carrito = [];
addToCart.forEach(btn => {
btn.addEventListener('click', addToCarritoItem);});
function addToCarritoItem(e){
e.preventDefault();
const boton = e.target;
const item = boton.closest('.box');
const itemTitle = item.querySelector('.producto-titulo').textContent;
const itemPrice = item.querySelector('.precio').textContent;
const itemImg = item.querySelector('.imgCart').src;
const itemId = item.querySelector('.addToCart').getAttribute('data-id');
const nuevoProducto = {
title: itemTitle,
price: itemPrice,
image: itemImg,
id: itemId,
cantidad: 1
};
const existe = carrito.some(item => item.id === nuevoProducto.id);
console.log(existe);
if(existe){
const producto = carrito.map( item =>{
if (item.id === nuevoProducto.id){
item.cantidad++;
return item;
}else{
return item;
}
});
}else{
addItemCarrito(nuevoProducto);
}
}
function addItemCarrito(nuevoProducto){
carritoSumaTotal();
carrito.push(nuevoProducto);
renderCarrito(); //
}
function renderCarrito(){
// carritoEl.innerHTML = ""; //
while(carritoEl.firstChild){
carritoEl.removeChild(carritoEl.firstChild)
};
carrito.forEach((item) =>{
carritoEl.innerHTML += `
<li class="buyItem">
<img src=${item.image}>
<div class="productCartInfo">
<h5 class="prdTitle">${item.title}</h5>
<h6>${item.price}</h6>
<div class="qnty">
<div>
<button class="mbtn">-</button>
<span class="countOfProduct">${item.cantidad}</span>
<button class="pbtn">+</button>
</div>
<div><i class="fas fa-times-circle dltProduct" data-id="${item.id}"></i></div>
</div>
</div>
</li>
`
})
carritoSumaTotal();
}
When I click on a product i want the quantity to go up by 1 the function is working but is not showing in the cart unless I add another product that does not exist in the cart at that moment. Any help?
I tried but could not find the error. I think is the error is in the "if(existe){...}" but do not know where.
I edited it to add more code
const addToCart = document.querySelectorAll('.addToCart');
let carritoEl = document.querySelector('.ul');
let carrito = [];
addToCart.forEach(btn => {
btn.addEventListener('click', addToCarritoItem);});
function addToCarritoItem(e){
e.preventDefault();
const boton = e.target;
const item = boton.closest('.box');
const itemTitle = item.querySelector('.producto-titulo').textContent;
const itemPrice = item.querySelector('.precio').textContent;
const itemImg = item.querySelector('.imgCart').src;
const itemId = item.querySelector('.addToCart').getAttribute('data-id');
const nuevoProducto = {
title: itemTitle,
price: itemPrice,
image: itemImg,
id: itemId,
cantidad: 1
};
const existe = carrito.some(item => item.id === nuevoProducto.id);
console.log(existe);
if(existe){
const producto = carrito.map( item =>{
if (item.id === nuevoProducto.id){
item.cantidad++;
return item;
}else{
return item;
}
});
}else{
addItemCarrito(nuevoProducto);
}
}
function addItemCarrito(nuevoProducto){
carritoSumaTotal();
carrito.push(nuevoProducto);
renderCarrito(); //
}
function renderCarrito(){
// carritoEl.innerHTML = ""; //
while(carritoEl.firstChild){
carritoEl.removeChild(carritoEl.firstChild)
};
carrito.forEach((item) =>{
carritoEl.innerHTML += `
<li class="buyItem">
<img src=${item.image}>
<div class="productCartInfo">
<h5 class="prdTitle">${item.title}</h5>
<h6>${item.price}</h6>
<div class="qnty">
<div>
<button class="mbtn">-</button>
<span class="countOfProduct">${item.cantidad}</span>
<button class="pbtn">+</button>
</div>
<div><i class="fas fa-times-circle dltProduct" data-id="${item.id}"></i></div>
</div>
</div>
</li>
`
})
carritoSumaTotal();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有错误日志或任何演示,我认为:这是因为您没有在增加数量后调用
RenderCarrito
函数试图解释:
With no error logs or any demo, here is what I think: it is because you're not calling the
renderCarrito
function after you have incremented the quantity:Here is a snippet to help you understand what I am trying to explain: