简体   繁体   English

数组中每个项目的按钮,每个项目都有不同的 output

[英]button for each item in array and different output for each

I have 3 parallel arrays: fruit, colour, and shape我有 3 个平行的 arrays:水果、颜色和形状

I would like each of the fruits to be a button.我希望每个水果都是一个按钮。 When the button is pressed the colour and shape of the fruit displays below.按下按钮时,水果的颜色和形状将显示在下方。 I have managed to display the buttons but when run only the last colour and shape are displayed as an alert when the buttons are pressed.我设法显示了按钮,但是在运行时,只有最后一种颜色和形状在按下按钮时显示为警报。

Here's my code:这是我的代码:

var fruit = ["apples", "bananas", "oranges"];
var colour = ["red", "yellow", "orange"];
var shape = ["round", "long", "round"];

for(i = 0; i < fruit.length; i++) {
   holder = colour[i] + shape[i];
   document.getElementById("foo").innerHTML += "<button onclick='alert(" + 'holder' + ")'>" + fruit[i] + "</button>";

it does not seem to work when putting the colour and shape straight into the alert instead of the holder as shown:将颜色和形状直接放入警报而不是支架时,它似乎不起作用,如图所示:

document.getElementById("foo").innerHTML += "<button onclick='alert(" + 'colour[i] + shape[i]' + ")'>" + fruit[i] + "</button>";
//does not work

note: the arrays are much longer so it would not work to do each item separately.注意:arrays 要长得多,所以单独做每个项目是行不通的。 Also it would be better if the colours and shapes were displayed below instead of in a alert.此外,如果颜色和形状显示在下方而不是警报中会更好。

thanks in advance!提前致谢!

 var fruit = ["apples", "bananas", "oranges"]; var colour = ["red", "yellow", "orange"]; var shape = ["round", "long", "round"]; for(i = 0; i < fruit.length; i++) { holder = colour[i] + shape[i]; document.getElementById("foo").innerHTML += '<button onclick="alert(\''+ holder + '\')">' + fruit[i] + '</button>'; }
 <div id="foo"></div>

Your issue is that your onclick is not being constructed properly, and is using the holder as a variable, instead of putting the literal string it has for each iteration into the onclick alert call.您的问题是您的 onclick 构造不正确,并且将holder用作变量,而不是将其为每次迭代所具有的文字字符串放入 onclick 警报调用中。 Fixing the construction of the onclick makes it use the literal value it was constructed with, instead of the global variable.修复 onclick 的构造使其使用构造它的文字值,而不是全局变量。

You'are overriding on each loop iteration the global holder variable.您将在每次循环迭代时覆盖全局holder变量。 So at the end of the processing the loop the holder variable holds the last values in the iteration ie.因此,在处理循环结束时, holder变量保存迭代中的最后一个值,即。 orangeround . orangeround

You need either embedd the value in the alert at the moment of creation or reference the correct state like:您需要在创建时将值嵌入警报中或引用正确的 state,例如:

"<button onclick='alert(\"" + holder + "\")'>" + fruit[i] + "</button>";

or create an object which you can reference ie.:或创建一个您可以参考的 object,即:

var fruit = ["apples", "bananas", "oranges"];
var colour = ["red", "yellow", "orange"];
var shape = ["round", "long", "round"];
var holder = {};
for(let i = 0; i < fruit.length; i++) {
   holder[i] = colour[i] + shape[i];
   document.getElementById("foo").innerHTML += "<button onclick='alert(" + 'holder['+i+']' + ")'>" + fruit[i] + "</button>";
}

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

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