首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解决-如何使用Javascript在一个循环中生成多个二维码?

解决-如何使用Javascript在一个循环中生成多个二维码?
EN

Stack Overflow用户
提问于 2020-06-03 12:24:00
回答 1查看 909关注 0票数 1

例如,我想用相同的信息生成10个不同的二维码。目前,我每次只能生成一个二维码。我应该放入一个循环还是任何解决方案?

代码语言:javascript
复制
    function makeCode () {  

        // All inputs that contain the value
        $qrs = $('.qr_value');
        
        // Create a new instance of the QRCode for each input
        $qrs.each(function(index, item){
            
            // We cant hace same id multiple times, so, we need to create dynamic ids,
            // thats why we are concatenating the index to the id string
            let containerQr =  "qrcode_"+index;

            // Create QR
            let qrcode = new QRCode(containerQr, {
                text: item.value, // value to read on qr
                width: 500,
                height: 500,
                colorDark : "#000000",
                colorLight : "#ffffff",
                correctLevel : QRCode.CorrectLevel.H,
            });
        });
    }

    makeCode();
代码语言:javascript
复制
// loop on any technology that u use, is php on my case
foreach($values as $key => $value){
    <input type="hidden" class="qr_value" value="$value">
    <div id="qrcode_$key"></div>
}

EN

回答 1

Stack Overflow用户

发布于 2020-06-03 12:43:18

使用一个简单的for()循环,并在生成二维码的代码周围迭代一个计数器。

代码语言:javascript
复制
let i = 1;
while( i <= 10){
    qrcode.clear(); // clear the code.
    qrcode.makeCode(); // make another code.
}

代码语言:javascript
复制
for(let i = 1; i <= 10; i++){
  // generate your qr code
  console.log(i)
}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62165203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档