💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
#### js页面监听扫码枪 > 该方法可以直接放在js代码中使用 ```javascript // 扫码枪 var code = ""; var lastTime, nextTime; var lastCode, nextCode; document.onkeypress = function (e) { if (window.event) { // IE nextCode = e.keyCode; } else if (e.which) { // Netscape/Firefox/Opera nextCode = e.which; } if (nextCode === 13) { if (code.length < 3) return; // 手动输入的时间不会让code的长度大于2,所以这里只会对扫码枪有 console.log(code); // 获取到扫码枪输入的内容,做别的操作 // 得到扫码枪的值,请求数据库,返回结果 $.ajax({ type: "POST", url: "{:url('barcode_select')}", data: {barcode: code}, dataType: "json", success: function (res) { if (res.code == 0){ var data = res.data var shop_name = ''; for(var i = 0; i < (data.shopinfo).length; i++){ shop_name = data.shopinfo[i].name + '和' + shop_name } var shop_str = shop_name.substr(0, shop_name.length-1); // 给搜索框赋值并搜索 $("#keys").attr("value", data.barcode); $("#search").click(); // 拼接需要语音播报的值 var str = data.goods_name + data.color + data.size_name + '属于' +shop_str // 调用语音播报的方法 speckText(str); }else { console.log('111') } },error: function (error) { console.log(11) } }); code = ''; lastCode = ''; lastTime = ''; return; } nextTime = new Date().getTime(); if (!lastTime && !lastCode) { code += e.key; } if (lastCode && lastTime && nextTime - lastTime > 30) { // 当扫码前有keypress事件时,防止首字缺失 code = e.key; } else if (lastCode && lastTime) { code += e.key; } lastCode = nextCode; lastTime = nextTime; } ```