這篇文章主要講解了如何使用JavaScript獲取掃碼槍掃描得到的條形碼,內(nèi)容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
成都創(chuàng)新互聯(lián)公司為客戶提供專業(yè)的網(wǎng)站設(shè)計制作、網(wǎng)站制作、程序、域名、空間一條龍服務,提供基于WEB的系統(tǒng)開發(fā). 服務項目涵蓋了網(wǎng)頁設(shè)計、網(wǎng)站程序開發(fā)、WEB系統(tǒng)開發(fā)、微信二次開發(fā)、成都手機網(wǎng)站制作等網(wǎng)站方面業(yè)務。下面通過實例代碼給大家介紹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 對應鍵碼值 48-57 //數(shù)字鍵盤 數(shù)字鍵0-9 對應鍵碼值 96-105 else if((nextCode>=48&&nextCode<=57) || (nextCode>=96&&nextCode<=105)){ //數(shù)字鍵盤的鍵碼值對應的字符有問題,所以手動調(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 = ''; html += ' '; } function getProductDetail(){ //獲取商品的詳細信息,然后賦值 }'; html += ' '; html += ' '; html += ' '; html += '