gpt4 book ai didi

javascript - 使用 qrcode.js OnClick 生成随机 QR 码

转载 作者:行者123 更新时间:2023-11-29 16:41:43 25 4
gpt4 key购买 nike

我正在使用qrcode.js用于生成二维码。我想每次单击“添加新选项卡”而不是添加新的二维码时生成随机二维码。我为随机数创建了一个代码,但是当我尝试添加 onclick 函数时,它添加了新的代码而不是随机数。

JSFIDDLE:https://jsfiddle.net/aice09/L8pp9336/
GITHUB:https://github.com/Ailyn09/project102/blob/master/qrcode.html
密码:https://codepen.io/aice09/pen/Ogqajr

$('#lithird').click(function() {

var qrcode = new QRCode("qrcode");

function makeCode() {

function randomNumber(len) {
var randomNumber;
var n = '';

for (var count = 0; count < len; count++) {
randomNumber = Math.floor(Math.random() * 10);
n += randomNumber.toString();
}
return n;
}

var value = randomNumber(13);

var elText = value;

qrcode.makeCode(elText);
}

makeCode();

});
#qrcode {
width:160px;
height:160px;
margin-top:15px;
}
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<script src="https://cdn.rawgit.com/davidshimjs/qrcodejs/gh-pages/qrcode.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-fluid" id="main">
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" id="lifirst" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">List</a></li>
<li role="presentation" id="lithird"><a href="#editable" aria-controls="editable" role="tab" data-toggle="tab" onclick='transfer_new()'>Add New</a></li>
</ul>
<!--/ Nav tabs /-->

<!-- Tab panes -->
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="home" style="margin-top: 10px;">
IN THE NEXT TABA THE AR CODE
</div>
<div role="tabpanel" class="tab-pane" id="editable">

<div class="col-xs-12 col-sm-12 col-md-4 col-lg-4 col-md-offset-4 col-lg-offset-4">
<div id="qrcode"></div>
</div>=
</div>
</div>
</div>

最佳答案

尝试添加 $('#qrcode').html(''); 作为 .click(function(){} 中的第一项

像这样:

$('#lithird').click(function(){      

$('#qrcode').html(''); // <---Add this, which should clear it out on the next click

var qrcode = new QRCode("qrcode");

function makeCode () {

function randomNumber(len) {
var randomNumber;
var n = '';

for(var count = 0; count < len; count++) {
randomNumber = Math.floor(Math.random() * 10);
n += randomNumber.toString();
}
return n;
}

var value = randomNumber(13);
var elText = value;

qrcode.makeCode(elText);
}

makeCode();

});

Codepen for full review

关于javascript - 使用 qrcode.js OnClick 生成随机 QR 码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45115027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com