简体   繁体   English

为什么将项目意外地多次而不是一次地推入数组?

[英]Why items are being pushed multiple times to the array unexpectedly instead of once?

I have a couple of checkboxes that are attached to an event and when clicked two values are being pushed to the ingredients and ingredientsPrice array respectively. 我有几个附加到事件的复选框,当单击时,两个值分别被推送到Ingredients和IngredientsPrice数组。 When I run it the first time the program seems to work fine however as I start a new order although the values are being reset somehow each item is being repetitively added. 第一次运行该程序时,尽管我以某种方式重置了值,但每次重复地添加每个项目,但该程序似乎都能正常运行。 I have noticed a pattern of the repetitive process that after each order every item is added for another one time like there is a loop. 我注意到重复过程的一种模式,在每个订单之后,每个项目又添加一次,就像有一个循环一样。 Small example below: 下面的小例子:

 var checkBoxes = document.getElementsByClassName("checkbox"); var addSandwich = document.getElementById("add-sandwich"); var ingredients = []; var ingredientsPrice = []; function getIngredientsInfo() { //loop through check boxes for (let i = 0; i < checkBoxes.length; i++) { //add event listener to check boxes checkBoxes[i].addEventListener("change", function() { if (event.target.checked) { //push the name of ingredient to ingredients array ingredients.push(event.target.name); //push the ingredient price to ingredientsPrice array ingredientsPrice.push(event.target.value); console.log(ingredients, ingredientsPrice + " current array values"); } }); } } function resetValues() { for (let x = 0; x < checkBoxes.length; x++) { checkBoxes[x].checked = false; } ingredients = []; ingredientsPrice = []; } getIngredientsInfo(); addSandwich.addEventListener("click", () => { getIngredientsInfo(); resetValues(); }); 
 <div class="checkbox-container"> <p>Ingredients</p> <fieldset class="hidden"> <legend>Choose your favourite ingredients</legend> <div class="checkbox-div"> <input type="checkbox" class="checkbox" id="bacon" name="bacon" value="1.00" /> <label class="label" for="bacon">Bacon</label> </div> <div class="checkbox-div"> <input type="checkbox" class="checkbox" id="beef" name="Beef" value="1.50"/> <label class="label" for="beef">Beef</label> </div> <div class="checkbox-div"> <input type="checkbox" class="checkbox" id="carrot" name="carrot" value="0.50"/> <label class="label" for="carrot">Carrot</label> </div> <div class="checkbox-div"> <input type="checkbox" class="checkbox" id="cucamber" name="cucamber" value=".25"/> <label class="label" for="cucamber">Cucamber</label> </div> <div class="checkbox-div"> <input type="checkbox" class="checkbox" id="duck" name="Duck" value="1.75"/> <label class="label" for="duck">Duck</label> </div> <div class="checkbox-div"> <input type="checkbox" class="checkbox" id="chicken" name="Grilled Chicken" value="2.00"/> <label class="label" for="chicken">Grilled Chicken</label> </div> <div class="checkbox-div"> <input type="checkbox" class="checkbox" id="ham" name="Ham" value=".5"/> <label class="label" for="ham">Ham</label> </div> <div class="checkbox-div"> <input type="checkbox" class="checkbox" id="lettuce" name="Lettuce" value="0"/> <label class="label" for="lettuce">Lettuce</label> </div> <div class="checkbox-div"> <input type="checkbox" class="checkbox" id="mushroom" name="Mushrooms" value=".5"/> <label class="label" for="mushroom">Mushrooms</label> </div> <div class="checkbox-div"> <input type="checkbox" class="checkbox" id="hard-boiled-egg" name="Hard Boiled Egg" value=".5" /> <label class="label" for="hard-boiled-egg">Hard Boiled Egg</label> </div> </fieldset> <div class="order-nav"> <button type="submit" id="add-sandwich" class="btn pd-x-10" value="submit">Add Sandwich</button> </div> </div> 

var arr = []; var arr = []; arr.push(aa, bb); arr.push(aa,bb); <---arr have value of aa and bb now however if I start a new order each item will be added twice.If I start another one after that each item is added three times and it keeps increasing every time <--- arr现在具有aa和bb的值,但是如果我开始一个新订单,则每个项目将被添加两次。如果我再启动一个新订单,则每个项目将被添加三次,并且每次都在增加

By addSandwich , you create a new event listener for every checkbox. 通过addSandwich ,您可以为每个复选框创建一个新的事件侦听器。

Just delete getIngredientsInfo(); 只需删除getIngredientsInfo();

addSandwich.addEventListener("click", () => {
    // getIngredientsInfo();
    resetValues();
});

 var checkBoxes = document.getElementsByClassName("checkbox"); var addSandwich = document.getElementById("add-sandwich"); var ingredients = []; var ingredientsPrice = []; function getIngredientsInfo() { //loop through check boxes for (let i = 0; i < checkBoxes.length; i++) { //add event listener to check boxes checkBoxes[i].addEventListener("change", function() { console.log(event.target.checked) if (event.target.checked) { //push the name of ingredient to ingredients array ingredients.push(event.target.name); //push the ingredient price to ingredientsPrice array ingredientsPrice.push(event.target.value); console.log(ingredients, ingredientsPrice + " current array values"); } }); } } function resetValues() { for (let x = 0; x < checkBoxes.length; x++) { checkBoxes[x].checked = false; } ingredients = []; ingredientsPrice = []; } getIngredientsInfo(); addSandwich.addEventListener("click", () => { // getIngredientsInfo(); resetValues(); }); 
 <div class="checkbox-container"> <p>Ingredients</p> <fieldset class="hidden"> <legend>Choose your favourite ingredients</legend> <div class="checkbox-div"> <input type="checkbox" class="checkbox" id="bacon" name="bacon" value="1.00" /> <label class="label" for="bacon">Bacon</label> </div> <div class="checkbox-div"> <input type="checkbox" class="checkbox" id="beef" name="Beef" value="1.50"/> <label class="label" for="beef">Beef</label> </div> <div class="checkbox-div"> <input type="checkbox" class="checkbox" id="carrot" name="carrot" value="0.50"/> <label class="label" for="carrot">Carrot</label> </div> <div class="checkbox-div"> <input type="checkbox" class="checkbox" id="cucamber" name="cucamber" value=".25"/> <label class="label" for="cucamber">Cucamber</label> </div> <div class="checkbox-div"> <input type="checkbox" class="checkbox" id="duck" name="Duck" value="1.75"/> <label class="label" for="duck">Duck</label> </div> <div class="checkbox-div"> <input type="checkbox" class="checkbox" id="chicken" name="Grilled Chicken" value="2.00"/> <label class="label" for="chicken">Grilled Chicken</label> </div> <div class="checkbox-div"> <input type="checkbox" class="checkbox" id="ham" name="Ham" value=".5"/> <label class="label" for="ham">Ham</label> </div> <div class="checkbox-div"> <input type="checkbox" class="checkbox" id="lettuce" name="Lettuce" value="0"/> <label class="label" for="lettuce">Lettuce</label> </div> <div class="checkbox-div"> <input type="checkbox" class="checkbox" id="mushroom" name="Mushrooms" value=".5"/> <label class="label" for="mushroom">Mushrooms</label> </div> <div class="checkbox-div"> <input type="checkbox" class="checkbox" id="hard-boiled-egg" name="Hard Boiled Egg" value=".5" /> <label class="label" for="hard-boiled-egg">Hard Boiled Egg</label> </div> </fieldset> <div class="order-nav"> <button type="submit" id="add-sandwich" class="btn pd-x-10" value="submit">Add Sandwich</button> </div> </div> 

You are adding an event listener for every click on addSandwich which results in the function being called multiple times. 您要为addSandwich每次单击添加一个事件侦听器,这将导致该函数被多次调用。 What you need to do is don;t call getIngredientsInfo() in the click listener for addSandwitch . 您需要做的是不要在click侦听器中为addSandwitch调用getIngredientsInfo() Change addSandwich.addEventListener("click", () => { getIngredientsInfo(); resetValues(); }); 更改addSandwich.addEventListener("click", () => { getIngredientsInfo(); resetValues(); }); to addSandwich.addEventListener("click", () => { resetValues(); }); addSandwich.addEventListener("click", () => { resetValues(); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM