简体   繁体   English

当我按下按钮时,它会给我一个结果,而不是我拥有的所有地图结果。 我只得到值 1

[英]When I press the button it gives me one result and not all the Map results I have. I only get value 1

I need help.我需要帮助。

There's something in the code that's not working properly for me.代码中的某些内容对我来说无法正常工作。

When I press a button it pulls me value of value of one product and every other button just does not pull their figure.当我按下一个按钮时,它会拉动我一种产品的价值,而其他每个按钮都不会拉动它们的数字。

const itemadd = document.querySelectorAll('.item-this');
let currentAccount;
let proteinValue;

const foods = new Map([
    ['קוטג',23],
    ['ביצה',11],
    ['יוגורט Pro',20],
    ['טונה',23]
]);


foods.forEach(function (value, index, arr) {
  const html = `<li class="">
      <strong>${index}</strong> כמות חלבון ${(proteinValue =
    value)}</li><button class="btn btn-primary item-this">הוסף</button><hr>`;
  items.insertAdjacentHTML('afterbegin', html);
});
const itemadd = document.querySelectorAll('.item-this');
for (let i = 0; i < itemadd.length; i++)
  itemadd[i].addEventListener('click', function () {
    console.log(proteinValue);
  });




When I press the button it only records the figure of ['tuna', 23] Intention 23 and all the other entries do not print them for me that I press the button当我按下按钮时,它只记录 ['tuna', 23] Intention 23 的数字,所有其他条目都不会为我打印它们,因为我按下了按钮

You can attach event handler to the button directly and pass the corresponding value.您可以直接将事件处理程序附加到按钮并传递相应的值。

let currentAccount={};
let proteinValue;

const foods = new Map([
    ['קוטג',23],
    ['ביצה',11],
    ['יוגורט Pro',20],
    ['טונה',23]
]);


foods.forEach(function(value,index,arr){

const html = `<li class=""> <button id="add" class="btn btn-primary" onclick="test(${value})" data-protein="${value}">הוסף</button>
<strong>${index}</strong> כמות חלבון ${proteinValue = value}</li><hr>`
items.insertAdjacentHTML('afterbegin', html);

})

function test(val){
    console.log(val);
}

or with eventListener, you can use data attribute.或者使用 eventListener,您可以使用 data 属性。

let currentAccount={};
let proteinValue;

const foods = new Map([
    ['קוטג',23],
    ['ביצה',11],
    ['יוגורט Pro',20],
    ['טונה',23]
]);


foods.forEach(function(value,index,arr){

const html = `<li class=""> <button id="add" class="btn btn-primary" data-protein="${value}">הוסף</button>
<strong>${index}</strong> כמות חלבון ${proteinValue = value}</li><hr>`
items.insertAdjacentHTML('afterbegin', html);

})

document.querySelectorAll('.btn-primary').forEach((ele) => {
    ele.addEventListener('click', function(e){
        console.log(e.target.getAttribute('data-protein'))
    })
})

暂无
暂无

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

相关问题 我有十个我想要的按钮,当我按下一个按钮时,所有按钮都停止 - I have ten buttons I want when I press a button all buttons stop 我想在提示中按下 Esc 按钮时提醒一个值,但我不知道...(下面的代码) - I want to alert a value when I press the Esc button in prompt, but I have no idea... (code below) 当 map 的条件为真时,我只需要将结果分配给“值” - I need to assign to “value” only the result when the condition of the map is true 每当我按下播放按钮时,控制台都会给我一个“无法读取 'play' 的 null 属性” - Whenever I press the play button, console gives me a “can't read property of 'play' of null” 是否有可能当我按下按钮 1 时,红色按钮也变成了一个? - Is it possible that when i press button 1 the red button becomes one too? 在jQuery中,当它们都具有相同的名称时,如何获得单选按钮的值? - In jQuery, how do I get the value of a radio button when they all have the same name? 当我按下“全部”复选框时“我没有得到任何数据。我在这里做错了什么? - When I press the “All” checkbox" I don't get any data. What have I done wrong here? 脚本仅显示一个结果,如何在ap标签中输出所有结果? - Script is only showing one result how can I output all the results inside a p tag? 如何仅在用户按下 Enter 按钮时进行搜索? - How do I search only when user press enter button? 当我按回车键时,我会得到isNaN,但值是一个数字 - When I press enter I get isNaN, but the value is a number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM