您好,登錄后才能下訂單哦!
這篇文章主要講解了如何使用JavaScript獲取掃碼槍掃描得到的條形碼,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
下面通過(guò)實(shí)例代碼給大家介紹js掃碼槍掃描條形碼的實(shí)現(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 對(duì)應(yīng)鍵碼值 48-57 //數(shù)字鍵盤 數(shù)字鍵0-9 對(duì)應(yīng)鍵碼值 96-105 else if((nextCode>=48&&nextCode<=57) || (nextCode>=96&&nextCode<=105)){ //數(shù)字鍵盤的鍵碼值對(duì)應(yīng)的字符有問(wèn)題,所以手動(dò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(){ //獲取商品的詳細(xì)信息,然后賦值 }
思路:
1.注冊(cè)onkeydown事件,捕獲數(shù)字鍵的按下事件
2.計(jì)算按下數(shù)字鍵的時(shí)間間隔,若間隔小于30毫秒,則為掃碼槍輸入
3.捕獲Enter案件的按下事件,判斷捕獲的掃碼槍輸入數(shù)值是否為空,不為空,對(duì)相應(yīng)的文本框賦值,同時(shí)觸發(fā)按找條形碼查找商品的方法
看完上述內(nèi)容,是不是對(duì)如何使用JavaScript獲取掃碼槍掃描得到的條形碼有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。