<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>扫码枪</title>
</head>
<body>
<form>
<table>
<tr>
<td>生成型号:</td>
<td><input type="text" id="model" value=""/></td>
</tr>
<tr>
<td>批次号:</td>
<td><input type="text" id="batch" value=""/></td>
</tr>
<tr>
<td>订单编号:</td>
<td><input type="text" id="number" value=""/></td>
</tr>
<tr>
<td>版本:</td>
<td><input type="text" id="edition" value=""/></td>
</tr>
</table>
</form>
</body>
<script type="text/javascript" src="http://bqweb.tpddns.cn:8765/publicjs/js/jquery-3.2.1.min.js"></script>
<script>
var code = '';
var lastTime = 0;
var name="",dept="",post="",userno="";
// 清空临时变量
function clearCode() {
code = '';
lastTime = 0;
}
window.addEventListener('keypress', function(e) {
var currCode = e.which;
var currTime = new Date().getTime();
if (lastTime > 0) {
if (currTime - lastTime <= 300) {
code += String.fromCharCode(currCode);
} else if (currTime - lastTime > 1000) {
// 超时清空
clearCode();
}
}
// 第一次按键
else {
code = String.fromCharCode(currCode);
}
lastTime = currTime;
// 遇到回车
if (currCode == 13) {
if (code.indexOf("model:")!=-1){
$('#model').val(code.split("model:")[1]);
}
if (code.indexOf("batch:")!=-1){
$('#batch').val(code.split("batch:")[1]);
}
if (code.indexOf("number:")!=-1){
$('#number').val(code.split("number:")[1]);
}
if (code.indexOf("edition:")!=-1){
$('#edition').val(code.split("edition:")[1]);
}
clearCode();
}
});
</script>
</html>
二维码格式: model:00001
batch:00002
number:00003
edition:00004
注:需换行