当你需要一个二维码,二维码里有你想表示的内容或者想进入的网页的时候应该怎么办呢?
今天看看一个随机二维码的生成:
所用工具:
Microsoft Visual Studio 2010
SQL Server Management Studio
首先:我们得引入俩个库:
<script type="text/javascript" src="http://static.runoob.com/assets/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript" src="http://static.runoob.com/assets/qrcode/qrcode.min.js"></script>
然后,我们得引入一个控件:
<asp:DropDownList ID="DropDownList2" runat="server" Height="16px" Width="322px"
AutoPostBack="True" onchange="makeCode()" >
</asp:DropDownList>
该控件是一个下拉列表控件 οnchange="makeCode()"方法是调用下面的JS方法
接下来就是生成二维码的JS方法了
<script type="text/javascript">
var qrcode = new QRCode(document.getElementById("qrcode"), {
width: 100,
height: 100
});
function makeCode() {
var elText = document.getElementById("DropDownList2");
if (!elText.value) {
elText.focus();
return;
}
qrcode.makeCode(elText.value);
}
makeCode();
$("#text").
on("blur", function () {
makeCode();
}).
on("keydown", function (e) {
if (e.keyCode == 13) {
makeCode();
}
});
</script>
里面的DropDownList2是调用了数据库里面的数据传入的是DropDownList2.SelectedValue.
如果不懂这个下拉的方法,可以直接使用输入框的方法。
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko">
<head>
<title>Javascript 二维码生成器:QRCode</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no" />
<script type="text/javascript" src="http://static.runoob.com/assets/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript" src="http://static.runoob.com/assets/qrcode/qrcode.min.js"></script>
</head>
<body>
<input id="text" type="text" value="http://www.runoob.com" style="width:80%" /><br />
<div id="qrcode" style="width:100px; height:100px; margin-top:15px;"></div>
<script type="text/javascript">
var qrcode = new QRCode(document.getElementById("qrcode"), {
width : 100,
height : 100
});
function makeCode () {
var elText = document.getElementById("text");
if (!elText.value) {
alert("Input a text");
elText.focus();
return;
}
qrcode.makeCode(elText.value);
}
makeCode();
$("#text").
on("blur", function () {
makeCode();
}).
on("keydown", function (e) {
if (e.keyCode == 13) {
makeCode();
}
});
</script>
</body>
</html>
也是需要引入jquery.min.js和qrcode.min.js两个库