search

Home  >  Q&A  >  body text

javascript - Problems with bootstarp-fileinput initialization preview

I want to directly display the existing image when initializing fileinput. This image is accessed from ajax and is an array.

$.get(url,function(res){
//图片地址是res.img,这是一个数组
    $("#fileupload").fileinput({
        initialPreview: [
            //发现这里不能使用for循环res.img,应该怎么办?
        ],
        initialPreviewAsData: true,
        deleteUrl: "/site/file-delete",
        overwriteInitial: true,
    })
})

The following is the prototype:

$.get(url,function(res){
    $("#fileupload").fileinput({
        initialPreview: [
            'http://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/FullMoon2010.jpg/631px-FullMoon2010.jpg',
            'http://upload.wikimedia.org/wikipedia/commons/thumb/6/6f/Earth_Eastern_Hemisphere.jpg/600px-Earth_Eastern_Hemisphere.jpg'
        ],
        initialPreviewAsData: true,
        deleteUrl: "/site/file-delete",
        overwriteInitial: true,
    })
})

The following is json data

{goods_id: 9, goods_name: "产品测试4", cat_id: 40, sku_id: "888888888888", goods_encode: "77777777777",…}
addtime:1496837803
cat_id:40
goods_barcode:"66666666666"
goods_desc:null
goods_encode:"77777777777"
goods_id:9
goods_image:"/uploads/product/20170607/d60bf37e6c381c8b52be22e63217bc1c.jpg;/uploads/product/20170607/2cb078c49125ff42fbf13bee19a0e64c.jpg;"
goods_name:"产品测试4"
img:["/uploads/product/20170607/d60bf37e6c381c8b52be22e63217bc1c.jpg",…]
0:"/uploads/product/20170607/d60bf37e6c381c8b52be22e63217bc1c.jpg"
1:"/uploads/product/20170607/2cb078c49125ff42fbf13bee19a0e64c.jpg"
index:1
size:["37", "33", "35", "38", "39"]
sizes:"37,33,35,38,39"
sku_id:"888888888888"
status:1
updatetime:null

InitialPreview can obviously receive arrays directly. My json data above also shows that img is indeed an array, but why after writing like this, all the arrays are merged into one?

$.get(url,function(res){
    $("#fileupload").fileinput({
        initialPreview: [
            res.img
        ],
        initialPreviewAsData: true,
        deleteUrl: "/site/file-delete",
        overwriteInitial: true,
    })
})

The final result is

<img src="/uploads/product/20170607/d60bf37e6c381c8b52be22e63217bc1c.jpg,/uploads/product/20170607/2cb078c49125ff42fbf13bee19a0e64c.jpg" class="file-preview-image kv-preview-data" title="Desert.jpg" alt="Desert.jpg" style="width:auto;height:160px;">

The correct one should be two pictures

代言代言2737 days ago805

reply all(1)I'll reply

  • 代言

    代言2017-06-26 10:58:57

    initialPreview: res.img,

    reply
    0
  • Cancelreply