js獲取USB掃碼槍數(shù)據(jù)的方法
本文實(shí)例為大家分享了js獲取USB掃碼槍數(shù)據(jù)的具體過程,供大家參考,具體內(nèi)容如下
廢話不多說,直接上代碼,這個(gè)方法避免了 首字缺失,字符串亂碼等等問題,特別好用,記錄一下
中間的Ajax可直接調(diào)用登錄功能,實(shí)現(xiàn)自動(dòng)登錄
說明:輸入框需要獲得焦點(diǎn),這個(gè)是必須的,其次為了防止回車觸發(fā)手動(dòng)登錄,需要添加οnkeypress="if(event.keyCode==13) return false;"這個(gè)操作,這個(gè)在回車的時(shí)候,輸入框焦點(diǎn)不會(huì)丟失,除非手動(dòng)切換焦點(diǎn),或者調(diào)用focus();方法
<div class="form-group"> <label for="inputUsernameEmail">賬號(hào)</label> <input type="text" placeholder="請(qǐng)輸入賬號(hào)" name="username" id="loginname" class="form-control" autofocus onkeypress="if(event.keyCode==13) return false;"> </div>
代碼:
<script> window.onload = (e)=> { this.start = new Date().getTime() let code = '' let lastTime, nextTime let lastCode, nextCode let that = this window.document.onkeypress = function (e) { if (window.event) { // IE nextCode = e.keyCode } else if (e.which) { // Netscape/Firefox/Opera nextCode = e.which } console.log('nextCode', nextCode) if (e.which === 13 || window.event === 13) { var deviceCode = code; console.log(code) console.log('掃碼結(jié)束') console.timeEnd() code = '' lastCode = '' lastTime = '' $.ajax({ cache: true, type: "POST", url: rootPath + "/admin/login", data: {code: deviceCode, type: 1}, async: false, error: function (request) { $("#loginname").val(""); $.modal.alertError("系統(tǒng)錯(cuò)誤"); }, success: function (data) { $("#loginname").val(""); if (data.code == 200) { location.href = rootPath + '/admin/index'; } else { $.modal.alertError(data.msg); } } }); } nextTime = new Date().getTime() if (!lastTime && !lastCode) { console.log('掃碼開始。。。') code += e.key } if (lastCode && lastTime && nextTime - lastTime > 500) { // 當(dāng)掃碼前有keypress事件時(shí),防止首字缺失 console.log('防止首字缺失。。。') code = e.key } else if (lastCode && lastTime) { console.log('掃碼中。。。') code += e.key } lastCode = nextCode lastTime = nextTime } } </script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
uniapp實(shí)現(xiàn)地圖點(diǎn)聚合功能的詳細(xì)教程
最近公司項(xiàng)目需求需要對(duì)設(shè)備在地圖上面進(jìn)行監(jiān)控,并在當(dāng)設(shè)備一定距離時(shí)進(jìn)行聚合,這篇文章主要給大家介紹了關(guān)于uniapp實(shí)現(xiàn)地圖點(diǎn)聚合功能的相關(guān)資料,需要的朋友可以參考下2022-12-12分別用兩個(gè)函數(shù)實(shí)現(xiàn)的菜單
分別用兩個(gè)函數(shù)實(shí)現(xiàn)的菜單...2007-05-05JavaScript實(shí)現(xiàn)的級(jí)聯(lián)算法示例【省市二級(jí)聯(lián)動(dòng)功能】
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的級(jí)聯(lián)算法,結(jié)合省市二級(jí)聯(lián)動(dòng)下拉菜單功能實(shí)例分析了javascript事件響應(yīng)與元素動(dòng)態(tài)操作實(shí)現(xiàn)級(jí)聯(lián)算法的相關(guān)技巧,需要的朋友可以參考下2018-12-12js實(shí)現(xiàn)數(shù)組和對(duì)象的深淺拷貝
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)數(shù)組和對(duì)象的深淺拷貝,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09JavaScript實(shí)現(xiàn)生成動(dòng)態(tài)表格和動(dòng)態(tài)效果的方法詳解
這篇文章主要介紹了如何通過JavaScript語言實(shí)現(xiàn)動(dòng)圖表格的生成以及動(dòng)態(tài)效果的實(shí)現(xiàn),文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-02-02利用H5api實(shí)現(xiàn)時(shí)鐘的繪制(javascript)
這篇文章主要為大家詳細(xì)介紹了利用H5api實(shí)現(xiàn)時(shí)鐘的繪制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09