這篇文章主要講解了如何使用JavaScript獲取掃碼槍掃描得到的條形碼,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。
成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供廣平企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計、做網(wǎng)站、H5高端網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為廣平眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進行中。
下面通過實例代碼給大家介紹js掃碼槍掃描條形碼的實現(xiàn)方法,具體代碼如下所示:
var keycode = ""; var lastTime=null,nextTime; var lastCode=null,nextCode; document.οnkeydοwn=function(e){ if(window.event){ // IE nextCode = e.keyCode } else if(e.which){ // Netscape/Firefox/Opera nextCode = e.which } //+鍵,增加新數(shù)據(jù)行 if(nextCode==107 || nextCode==187){ addNewGoodLine(); } //-鍵,刪除最后一條數(shù)據(jù)行 else if(nextCode==109 || nextCode==189){ $(".new_products:last").remove(); } //字母上方 數(shù)字鍵0-9 對應(yīng)鍵碼值 48-57 //數(shù)字鍵盤 數(shù)字鍵0-9 對應(yīng)鍵碼值 96-105 else if((nextCode>=48&&nextCode<=57) || (nextCode>=96&&nextCode<=105)){ //數(shù)字鍵盤的鍵碼值對應(yīng)的字符有問題,所以手動調(diào)整鍵碼值 var codes = {'48':48,'49':49,'50':50,'51':51,'52':52,'53':53,'54':54,'55':55,'56':56,'57':57, '96':48,'97':49,'98':50,'99':51,'100':52,'101':53,'102':54,'103':55,'104':56,'105':57 }; nextCode = codes[nextCode]; nextTime = new Date().getTime(); if(lastCode == null && lastTime == null) { keycode = String.fromCharCode(nextCode); } else if(lastCode != null && lastTime != null && nextTime - lastTime <= 30) { keycode += String.fromCharCode(nextCode); } else{ keycode = ""; lastCode = null; lastTime = null; } lastCode = nextCode; lastTime = nextTime; } //13 為按鍵Enter else if(nextCode==13 && keycode!= ""){ var code = $(".new_products:last .code").val(); if(code != ""){ //最后一行已錄入數(shù)據(jù),重新生成新行 addNewGoodLine(); } $(".new_products:last .code").val(keycode).blur(); keycode = ""; lastCode = null; lastTime = null; } } function addNewGoodLine(){ //生成新數(shù)據(jù)行 var html = '<tr class="new_products">'; html += ' <td></td>'; html += ' <td>'; html += ' <input type="text" class="code" οnblur="getProductDetail()" />'; html += ' </td>'; html += '</tr>'; } function getProductDetail(){ //獲取商品的詳細信息,然后賦值 }
思路:
1.注冊onkeydown事件,捕獲數(shù)字鍵的按下事件
2.計算按下數(shù)字鍵的時間間隔,若間隔小于30毫秒,則為掃碼槍輸入
3.捕獲Enter案件的按下事件,判斷捕獲的掃碼槍輸入數(shù)值是否為空,不為空,對相應(yīng)的文本框賦值,同時觸發(fā)按找條形碼查找商品的方法
看完上述內(nèi)容,是不是對如何使用JavaScript獲取掃碼槍掃描得到的條形碼有進一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁名稱:如何使用JavaScript獲取掃碼槍掃描得到的條形碼
文章地址:http://www.rwnh.cn/article34/ipcese.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、網(wǎng)站排名、App設(shè)計、全網(wǎng)營銷推廣、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)