jQuery输入域名网址生成二维码
XML/HTML Code
- <form action="" class="form-horizontal panel-body" id="form1" method="post">
- <ul class="nav nav-tabs nav-tabs-sm" role="tablist" id="mytabs">
- <li role="presentation" class="active">
- <a href="" role="tab" data-toggle="tab" id="tab1" aria-controls="pane1"><strong>QRCode Generator</strong></a>
- </li>
- </ul>
- <div class="tab-content" style="padding-top: 15px;">
- <div role="tabpanel" class="tab-pane fade in active" id="pane1" aria-labelledby="tab1">
- <div class="form-group form-group-sm">
- <label class="control-label col-md-2">二维码内容</label>
- <div class="col-md-10">
- <textarea name="txtContent" id="txtContent" rows="2" class="form-control input-normal" placeholder="">http://www.freejs.net/</textarea>
- </div>
- </div>
- <div class="form-group form-group-sm">
- <label class="control-label col-md-2">二维码图像</label>
- <div class="col-md-10">
- <div id="qrcode"></div>
- <p class="m-top-10 text-muted">使用手机扫一扫</p>
- </div>
- </div>
- <div class="form-group form-group-sm">
- <div class="col-md-10 col-md-offset-2">
- <p class="form-control-static text-danger" id="tips"></p>
- </div>
- </div>
- </div>
- </div>
- <hr class="sm">
- <div class="btn-footer">
- <button name="btnCreate" class="btn btn-success" type="button" onClick="f_onCreateQrcode();" title="将根据您填写的内容生成二维码图像">开始生成</button>
- <button name="btnClear" class="btn btn-default" type="button" onClick="f_onClearQrcode();" title="清除">清除</button>
- </div>
- </form>
JavaScript Code
- <script type="text/javascript">
- //二维码内容
- var txtContent = document.getElementById("txtContent"), tips = $("#tips");
- // 设置 qrcode 参数
- var qrcode = new QRCode('qrcode', {
- text: 'www.freejs.net',
- width: 256,
- height: 256,
- colorDark: '#000000',
- colorLight: '#ffffff',
- correctLevel: QRCode.CorrectLevel.H
- });
- //生成二维码图像
- function f_onCreateQrcode() {
- if (!txtContent) {
- return false;
- } else {
- if (!txtContent.value) {
- tips.html("!请填写<strong>二维码内容</strong>再提交生成");
- } else {
- // 使用 API
- qrcode.clear();
- tips.empty();
- qrcode.makeCode(txtContent.value);
- }
- return false;
- }
- }
- //清除二维码内容
- function f_onClearQrcode() {
- if (txtContent) {
- txtContent.value = "www.freejs.net";
- // 使用 API
- qrcode.clear();
- qrcode.makeCode('请输入二维码内容再生成图像');
- }
- }
- </script>
原文地址:http://www.freejs.net/article_jquerywenzi_762.html