【问题标题】:Conditional statement limiting the number of displayed elements限制显示元素数量的条件语句
【发布时间】:2022-01-05 16:52:57
【问题描述】:

我无法实现限制我将相同商品添加到购物清单的逻辑。当商品相同时,我只想显示现有商品的数量。

    <div class="pizzas">
    </div>
    <div class="shoppingCart">
        <p class="totalPrice">Hungry? order our pizzas</p>
    </div>

// js

fetch("https://raw.githubusercontent.com/alexsimkovich/patronage/main/api/data.json")
.then(data => data.json())
.then(data => {
let valueCurrency = 0;
data.forEach(element => {
    const shoppingCart = document.querySelector(".shoppingCart");
    const pizzas = document.querySelector(".pizzas");
    const box = document.createElement("div");
    const img = document.createElement("img");
    const title = document.createElement("h3");
    const ingredients = document.createElement("p");
    const price = document.createElement("h4");
    const btn = document.createElement("button");
    const totalPrice = document.querySelector(".totalPrice");
     
    box.className = "box";
    ingredients.className = "ingredients"
    btn.className = "btn";
    img.src = element.image;
    img.className = "img";
     
    title.innerHTML = element.title;
    ingredients.innerHTML = element.ingredients;
    price.innerHTML = element.price.toFixed(2) + " zł";
    btn.innerHTML = "Dodaj do koszyka";
     
    box.appendChild(img);
    box.appendChild(title);
    box.appendChild(ingredients);
    box.appendChild(price);
    box.appendChild(btn);
    pizzas.appendChild(box);


    btn.addEventListener("click", (e) => {
        valueCurrency = valueCurrency + element.price;

        const pizza = document.createElement("div");
        pizza.className = "pizzaList";
        const pizzasList = document.createElement("li");
        const pizzaPrice = document.createElement("p");
        const btnRemove = document.createElement("button");
        btnRemove.innerText = "X";
         
        pizzasList.innerText = title.textContent;
        pizzaPrice.innerText = price.textContent;

        pizza.appendChild(pizzasList);
        pizza.appendChild(pizzaPrice);
        pizza.appendChild(btnRemove);

        totalPrice.innerText = "Całkowita cena: " + valueCurrency.toFixed(2);

        if(pizzasList.innerText === pizzasList.innerText)
        {
            // don't add another item to the list
            // just add +1 to existing element
        }
        else
        {
            // add an item to the list
            shoppingCart.prepend(pizza);

        }

        btnRemove.addEventListener("click", (e) => {
            pizza.remove();
            valueCurrency = valueCurrency - element.price;
            totalPrice.innerText = "Całkowita cena: " + valueCurrency.toFixed(2);
        })
    })

});
 
})
.catch(err => console.log(err));

我的问题正是在条件语句中,我不知道如何实现对相同比萨饼的计数选项。

提前感谢您的帮助。

【问题讨论】:

    标签: javascript html json if-statement fetch


    【解决方案1】:

    由于您为此使用 html 元素,您可以做的是在您的比萨元素中使用数据属性,并在每次需要时增加它。

    类似:

    if(pizzasList === pizzasList)
    {
      pizza.dataset.total = Number(pizza.dataset.total) + 1;
    }
    else
    {
      pizza.dataset.total = 1;
      shoppingCart.prepend(pizza);
    }
    

    然后只需使用pizza.dataset.total 即可检索总重复次数。

    【讨论】:

    • 不幸的是,条件拼写错误并且效果不佳
    • 你想知道在条件上或里面写什么吗?无论如何,我认为您的方法不是最好的,为什么要使用 html 元素来保存数据?为什么不只使用简单的数据结构,如对象或数组?
    • 我尝试将单个比萨饼添加到板上,但遇到了一些实施问题,就放手了。我认为我可以在出现相同披萨时以某种方式做到这一点,它只会添加到现有项目中,而不是再次添加到列表中。我想知道在条件下写什么。
    • 我认为您需要为您的披萨添加一些标识符,例如 ID,这样您就可以比较它们,看看它们是否已经被订购了
    • 我没有想法了,我只有这件事要实现,它放弃了一点
    猜你喜欢
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    相关资源
    最近更新 更多