使用JavaScript获取扫码枪扫描得到的条形码的思路代码详解
下面通过实例代码给大家介绍js扫码枪扫描条形码的实现方法,具体代码如下所示:
varkeycode="";
varlastTime=null,nextTime;
varlastCode=null,nextCode;
document.οnkeydοwn=function(e){
if(window.event){
//IE
nextCode=e.keyCode
}elseif(e.which){
//Netscape/Firefox/Opera
nextCode=e.which
}
//+键,增加新数据行
if(nextCode==107||nextCode==187){
addNewGoodLine();
}
//-键,删除最后一条数据行
elseif(nextCode==109||nextCode==189){
$(".new_products:last").remove();
}
//字母上方数字键0-9对应键码值48-57
//数字键盘数字键0-9对应键码值96-105
elseif((nextCode>=48&&nextCode<=57)||(nextCode>=96&&nextCode<=105)){
//数字键盘的键码值对应的字符有问题,所以手动调整键码值
varcodes={'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=newDate().getTime();
if(lastCode==null&&lastTime==null){
keycode=String.fromCharCode(nextCode);
}elseif(lastCode!=null&&lastTime!=null&&nextTime-lastTime<=30){
keycode+=String.fromCharCode(nextCode);
}else{
keycode="";
lastCode=null;
lastTime=null;
}
lastCode=nextCode;
lastTime=nextTime;
}
//13为按键Enter
elseif(nextCode==13&&keycode!=""){
varcode=$(".new_products:last.code").val();
if(code!=""){
//最后一行已录入数据,重新生成新行
addNewGoodLine();
}
$(".new_products:last.code").val(keycode).blur();
keycode="";
lastCode=null;
lastTime=null;
}
}
functionaddNewGoodLine(){
//生成新数据行
varhtml='';
html+=' ';
html+=' ';
html+=' ';
html+=' ';
html+='';
}
functiongetProductDetail(){
//获取商品的详细信息,然后赋值
}
思路:
1.注册onkeydown事件,捕获数字键的按下事件
2.计算按下数字键的时间间隔,若间隔小于30毫秒,则为扫码枪输入
3.捕获Enter案件的按下事件,判断捕获的扫码枪输入数值是否为空,不为空,对相应的文本框赋值,同时触发按找条形码查找商品的方法
总结
到此这篇关于使用JavaScript获取扫码枪扫描得到的条形码的思路代码详解的文章就介绍到这了,更多相关js扫码枪扫描条形码内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!