javascript - 如何比较姓名和购物车中的姓名

标签 javascript html

我试图不让购物车有重复的商品,而是希望增加数量。

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

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>

关于javascript - 如何比较姓名和购物车中的姓名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49648396/

相关文章:

python - 如何使用 xPath 获取 </ul> 和 <p> 之间的文本?

javascript - 如何在html游戏中保存进度

html - ionic : Floating Menu

javascript - 将查询参数与集合运算符一起使用

javascript - page() 未定义错误信息

javascript - LeetCode - 为什么这个函数返回未定义,尽管它刚刚记录了一个数组?

javascript - socket.io/socket.io.js 位置在哪里?

javascript - 如何在单击按钮时将对话框附加到页面的主 div 中

html - 新手问题,为什么我的div不 float ?

Javascript 警报 - 删除 "The page at ??? Says"