使用input输入框来接收扫码枪信息
style=“opacity: 0;” //隐藏控件
autofocus 聚焦事件,给input输入框添加默认聚焦事件
<input name="saoma" id="saoma" class="form-control" type="text" autofocus style="opacity: 0;"></input>
$(document).ready(function() {
//页面加载时强制给input添加聚焦事件,避免页面关闭再打开会丢失聚焦事件
$("#saoma").focus();
document.getElementById("saoma").addEventListener("keydown", function (event) {
console.log("获取扫码信息-----");
if (event.key === "Enter") {
// 当按下回车键时,获取输入框中的值
var inputValue = this.value;
console.log(inputValue);
// 清空输入框,以便于下次输入
this.value = "";
// 阻止默认的回车键行为,例如提交表单
event.preventDefault();
}
})
});