Generate Random QR Code OnClick Using qrcode.js

做~自己de王妃 提交于 2021-02-19 06:20:22

问题


I am using qrcode.js for generating qrcode. I want to generate random QR code every time I click into add new tab instead of add new qr code. I created a code for a random number but when I try to add onclick function it add new not to random it.

JSFIDDLE: https://jsfiddle.net/aice09/L8pp9336/
GITHUB: https://github.com/Ailyn09/project102/blob/master/qrcode.html
CODEPEN: 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>

回答1:


Try adding $('#qrcode').html(''); as the first item in the .click(function(){}

Like this:

$('#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




回答2:


You were missing a function transfer_new defined in your onclick that was causing a JavaScript error.

function transfer_new() {
    $('#qrcode').html('');
}

$('#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;
}
#qrcode img {
    margin: 15px;
    border: 5px solid black;
}
<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>



回答3:


Just clear the content before generating new

    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;
    }

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

            var value = randomNumber(13);
            $("#qrcode").html("");
            var qrcode = new QRCode("qrcode");
            qrcode.makeCode(value);

    });    
    #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" >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>



回答4:


$(".generatetext").click(function(e) {
    e.preventDefault();
    var x =  randomNumber(13);
    $('#output').html('').qrcode(x);
});

 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;
        }
#output{border:1px solid #eee;min-height:200px;width:200px;}
.container{padding:10px;}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.qrcode/1.0/jquery.qrcode.min.js"></script>
<div class="container">
  <form class="form-inline">
    <div class="form-group">
      <button class="btn btn-primary generatetext">Generate Random QR Code</button>
    </div>
  </form>
  <div class="container">
    <div id="output" class="text-center"></div>
  </div>
</div>



回答5:


$('#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>


来源:https://stackoverflow.com/questions/45115027/generate-random-qr-code-onclick-using-qrcode-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!