嗨,这是我的代码,当你输入文本时,它可以正常工作,它会转换成二维码,但我想让它自动运行。我有一个生成随机数的php函数,所以qrcode应该由该函数的值生成,而不是键入文本。

这是我的html页面代码:
<!DOCTYPE html>
<body>
<input id="text" type="text" value="http" style="width:80%" /><br />
<div id="qrcode"></div>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='http://davidshimjs.github.com/qrcodejs/qrcode.min.js'></script>
<script src="js/index.js"></script>
</body>
</html>这是我的javascript函数:
var qrcode = new QRCode("qrcode");
function makeCode () {
var elText = <?php echo random_str(6); ?> ;
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();
}
});这是我想要实现的php函数,这样就可以生成一个随机数,并在此基础上形成qrcode。
<?php
function random_str($length, $keyspace = '0123456789')
{
$str = '';
$max = mb_strlen($keyspace, '8bit') - 1;
for ($i = 0; $i < $length; ++$i) {
$str .= $keyspace[random_int(0, $max)];
}
return $str;
}
?>https://stackoverflow.com/questions/41329974
复制相似问题