【发布时间】:2020-01-01 23:26:44
【问题描述】:
TLDR:
如何更改通过each 循环附加到按钮的数组的值?
背景故事:
我正在创建一个包含各种不同图片的图片库。当用户登录时,我从数据库中检索数据(通过 ajax)向用户展示他或她上传的图片等。
但是在此过程中,我使用图像循环将所有图像附加到 div。
当用户单击加载的图像(此时文档已“准备好”-> 加载)时,在图像旁边检索到的数据显示在 modal 中。像这样:(注意:这是一个非常短的版本,有很多输入字段等都是这样创建的,但显示起来没什么用)。
- 图片是一个多维数组。 它看起来像这样:
images = [
0 => [
0 => 'here is the title',
1 => 'Here comes the image path',
2 => 'the ID of the image',
3 => 'This is the accessibility which I will later down the road try to change'
],
1 => [
//repeat the above basically, but with different values
]
]
我为每个图像创建一个div 框的方式如下:(注意,这是一个简短的版本,主要问题是evenListener 而不是我创建div 的方式。所以我不包括那部分代码)
//the images here is an array!!
$(images).each(function(key, array) {
let image = document.createElement('img');
image.setAttribute('class', 'rounded resizeForcer');
image.setAttribute('src', array[3]);
image.setAttribute('data-toggle', 'modal');
image.setAttribute('data-target', '#previewImageBig');
image.addEventListener('click', function() {
previewImage(array); //when clicked on the image, a modal will open up, while the modal opens the data will be passed to the modal and from there on I show the data
});
});
当用户单击图像本身时,它会将多维数组中的数据一起显示并附加到modal。但是,用户可以更新外部人员访问图像的方式(假设您要上传带有令牌的图像,因此只能通过 URL 访问)。
问题:
当我以用户身份更新图像的可访问性时,如果我希望我的朋友看到图片,它应该发回一个新链接供我复制粘贴,并且这个链接应该覆盖在数组中设置的链接addEventListener。所以假设我有 3 张图像,这意味着我在图像数组中有索引 0、1 和 2,每个索引中有 4 个不同的索引。我想要实现但失败的是获取array(基本上是我在创建div 时附加的索引)并更改第三个索引的值。因此,当我单击所需的image 以弹出我的modal 时,它的值将被覆盖,而不是显示在我更改可访问性(索引 3)参考之前获取的数据:显示多维数组结构的代码。
【问题讨论】:
-
您是否尝试在用户更改数组后重新排序?
-
不是真的,我只是想更改数组索引 3(第三个索引始终是应该更改的索引)当用户单击“更改可访问性”时,它应该发送新的通过 URL 访问图像,因此您可以将其发送给您的朋友,例如查看它。
标签: javascript jquery html arrays