简体   繁体   English

如何在 sweet alert window 中打印一个循环

[英]How print a loop inside of sweet alert window

I want to loop an array and show the result using sweet alert, I am new in this so I try the code below, but show me some like [object],[object]我想循环一个数组并使用 sweet alert 显示结果,我是新手所以我尝试下面的代码,但给我一些像 [object],[object]

swal({
  content:{
    element: "ul",
    attributes: {
      innerHTML:$.each(json.DATA.ARYPUB, function (key, img) {
                      "<li><img src='"+img.PATH+"'></li>"
       })
    }
  }
})

Can someone help me, please有谁可以帮助我吗

One approach could be generating the complete HTML outside the constructor of the alert and then set that html string to the related property. 一种方法是在alert的构造函数外部生成完整的HTML ,然后将该html字符串设置为相关属性。 Example: 例:

var html = "";

$.each(json.DATA.ARYPUB, function(key, img)
{
    html += "<li><img src='" + img.PATH + "'></li>"
});

swal({
    content:{
        element: "ul",
        attributes: {
            innerHTML: html
        }
    }
});

I faced the same issue so, I did this我遇到了同样的问题,所以我这样做了

const addOption = (arr) => {
  let optionItems;
  arr.forEach(item =>{

    optionItems += `<option value="${item}">${item}</option>`
  })
  return optionItems
}

//in swal 

 Swal.fire({
    html:addOption(Yourarray) 
    })

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

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