在项目中,需要用 扫码枪读取条形码信息,并把内容填入表单,实现表单自动提交,实现信息录入!
常用场景:
参加活动时 身份查询,活动资格查询;
html页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<script src="__STATIC__/home/js/jquery.min.js"></script>
</head>
<body class="hold-transition login-page">
<form id='but' action="appDetail.php" method="post" >
<div class="am-u-sm-12 am-u-md-6 " >
<div class="am-input-group am-input-group-sm">
<input type="text" id='aname' name="aname" placeholder="手机号" value="" class="am-form-field" >
<button class="am-btn" ></button>
</div>
</div>
</form>
</body>
<script>
window.onload = function(e){
var code = "";
var lastTime,nextTime;
var lastCode,nextCode;
document.onkeypress = function(e) {
nextCode = e.which;
nextTime = new Date().getTime();
if(lastCode != null && lastTime != null && nextTime - lastTime <= 30) {
code += String.fromCharCode(lastCode);
} else if(lastCode != null && lastTime != null && nextTime - lastTime > 100){
code = "";
}
lastCode = nextCode;
lastTime = nextTime;
}
this.onkeypress = function(e){
if(e.which == 13){
$('#aname').val(code); //自动填充到表单
$('#but').submit(); //自动提交指定的表单
console.log(code);
code = "";
}
}
}
</script>
</html>
这样用JQuery对扫描枪的扫描监控,实现了表单自动填充与提交
喜欢的点桃心,喜欢一下~