簡體   English   中英

使用js在購物車html頁面上顯示數量的問題

[英]problem with display of quantity on a cart html page using js

感謝您之前的幫助,現在總量完美。 我試圖將相同的原則應用於總價,但它顯示“nan”(“price += parseInt(monPanier[i].price);”)。 在添加此行之前,顯示的總價格僅為每個項目一份,即使有兩個黑色項目,3 個紅色項目和 4 個黃色項目,它被認為是總價格一黑 + 一紅 + 一黃。 謝謝您的幫助。

 let asynCompteur = 0; let quantity = 0; let price = 0; for (let i = 0; i < monPanier.length; i++) { fetch('http://localhost:3000/api/products/' + monPanier[i].id)/*appel de l'api*/ .then((reponse) => reponse.json()) .then((data) => { /*asynCompteur pour réguler l'itération*/ asynCompteur = asynCompteur + 1; //J'ajoute le prix et la quantité quantity += parseInt(monPanier[i].quantity); price += data.price; console.log('prix', asynCompteur, price, quantity); price += parseInt(monPanier[i].price); console.log(typeof monPanier[i].quantity); if (asynCompteur === monPanier.length) { document.querySelector("#totalQuantity").innerText = quantity; document.querySelector("#totalPrice").innerText = price; }

它部分工作。 數量正確更新並跟隨購物車的變化。所以,總數量的顯示是正確的。但是......奇怪的是,總價格只考慮每個項目之一,這意味着:即黑色項目(2次),黃色項目(3次),紅色項目(2次),總顯示數量:7(正確),總價格:錯誤(顯示一件黑色項目+一件黃色項目+一件紅色項目的總價格。謝謝您的寶貴幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM