如何在Ajax中的for循环中显示数组值

时间:2019-07-02 04:35:05

标签: javascript jquery ajax

我尝试了在图库中显示一些Ajax成功函数值。我编写了这段代码来爆炸数据库表的列值。

var mystr = html.data.images;
var myarr = mystr.split("|");
console.log(myarr);

这段代码使我得到这样的结果。

(2) ["1562037525.jpg", "1562037525.jpg"]
0: "1562037525.jpg"
1: "1562037525.jpg"
length: 2
__proto__: Array(0)

我想将此图像值显示到我的图像库src标签中。我这样尝试过。

$.each(myarr, function(index, value) {
          $('#project_img_gallery').html('<aside class="thumbnails"> <a href="#" class="selected small" data-big="/images/projects/myarr[index]">
             <div class="thumbnail-image" style="background-image: url(/images/projects/'+myarr[index]+')"></div>
           </a></aside><main class="primary" style="background-image: url('/images/projects/myarr[index]');"></main>');
        });

但是它不显示我的阵列图像。我该如何解决。

2 个答案:

答案 0 :(得分:0)

按此,

  

我想将此图像值显示在我的图片库src标签中

您一定想要这个

var myarr =  ["1562037525.jpg", "1562037525.jpg"];

$.each(myarr, function(index, value) {
   $('.project_img_gallery').append('<aside class="thumbnails"><a href="#" class="selected small" data-big="/images/projects/'+myarr[index]+'"> <div class = "thumbnail-image"                style = "background-image: url(/images/projects/'+myarr[index]+')"></div> </a></aside > <main class="primary" style="background-image: url(/images/projects/'+myarr[index]+')"></main>');
});

Demo

答案 1 :(得分:0)

这是我找出的正确答案:

var output = '';
       output += '<aside class="thumbnails">';
       for(var i = 0; i < myarr.length; i++){
          output += '<a href="#" class="selected small" data-big="/images/projects/'+myarr[i]+'">';
          output += '<div class="thumbnail-image" style="background-image: url(/images/projects/'+myarr[i]+')">';
          output += '</div>';
          output += '</a>';
          output += '</div>';
       }
       output += '</aside>';
       output += '<main class="primary" style="background-image: url(/images/projects/'+myarr[0]+');">';
       output += '</main>';
       $('#project_img_gallery').html(output);