我正在尝试不要使购物车中有重复的物品,而是希望数量增加。

如何比较购物车中的名称和即将从产品输入到购物车中的名称?



var total = document.getElementById('total');
var cart = document.getElementById("cart");
var totalPrice = 0;

function add(e) {
  var price = parseFloat(e.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.value);
  var quantity = parseFloat(e.previousSibling.previousSibling.value);
  var name = e.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.value;

  totalPrice += price * quantity;
  console.log("the name is " + name);

  cart.innerHTML += "<tr><td>" + name + "</td><td>" + quantity + "</td><td>" + price +
    "</td><td class='getT'>" + (price * quantity) +
    "</td><td class='btn btn-danger del'  id='del' onclick='del(this)'>Delete</td></tr>";

  total.innerHTML = "Total Price: " + totalPrice;
}

<div id="total">TOTAL</div>
<div id="cart"></div>

<h3>Blackberry</h3>
<input type="text" value="Blackberry" class="name" id="name" style="display: none;">
Price: 50,000
<input type="number" id="price" value="50000"><br />
Quantity <input type="number" id="quantity" min="1" max="50" value="1" s>
<button class="AddtoCart" onclick="add(this)">ADD TO CART</button>

<h3>Iphone 5</h3>
<input type="text" value="iphone 5" class="name" id="name" style="display: none;">
Price: 100,000
<input type="number" id="price" value="100000"><br />
Quantity <input type="number" id="quantity" min="1" max="50" value="1">
<button class="AddtoCart" onclick="add(this)">ADD TO CART</button>

<h3>Iphone 6</h3>
<input type="text" value="iphone 6" class="name" id="name" style="display: none;">
Price: 150,000
<input type="number" id="price" value="150000"><br />
Quantity <input type="number" id="quantity" min="1" max="50" value="1">
<button class="AddtoCart" onclick="add(this)">ADD TO CART</button>

最佳答案

var total = document.getElementById('total');

var cart = document.getElementById("cart");
var totalPrice = 0;
var productsCart={};
function add(e) {

    var price = parseFloat(e.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.value);
    var quantity = parseFloat(e.previousSibling.previousSibling.value);
    var name = e.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.value;

    totalPrice += price * quantity;
    console.log("the name is " + name);
    if(productsCart[name]==null){
        productsCart[name]={"name":name,"quantity":quantity,"price":price};
    }else{
        productsCart[name]={"name":name,"quantity":quantity+parseInt(productsCart[name].quantity),"price":price};
    }
    var inn="";
    for(var names in productsCart){
        inn+="<tr><td>"+productsCart[names].name+"--</td><td>"+productsCart[names].quantity+"--</td><td>"+productsCart[names].price+ "--</td><td class='getT'>" + (productsCart[names].price * productsCart[names].quantity) + "--</td><td class='btn btn-danger del'  id='del' onclick='del(this)'>Delete</td>-----</tr>";
    }
    cart.innerHTML =inn;
    total.innerHTML = "Total Price: " + totalPrice;
}


HTML

<div id="total">TOTAL</div>
<div id="cart"></div>


<h3>Blackberry</h3>
<input type="text" value="Blackberry" class="name" id="name" style="display: none;">
Price: 50,000<input type="number" id="price" value="50000"><br>
Quantity <input type="number" id="quantity" min="1" max="50" value="1"s>
<button class="AddtoCart" onclick="add(this)">ADD TO CART</button>

<h3>Iphone 5</h3>
<input type="text" value="iphone 5" class="name" id="name" style="display: none;">
Price: 100,000<input type="number" id="price" value="100000"><br>
Quantity <input type="number" id="quantity" min="1" max="50" value="1">
<button class="AddtoCart" onclick="add(this)">ADD TO CART</button>

<h3>Iphone 6</h3>
<input type="text" value="iphone 6" class="name" id="name" style="display: none;"> Price: 150,000
<input type="number" id="price" value="150000"><br>
Quantity <input type="number" id="quantity" min="1" max="50" value="1">
<button class="AddtoCart" onclick="add(this)">ADD TO CART</button>

09-27 10:56