This article shares the specific process of js obtaining USB barcode scanner data for your reference. The specific content is as follows Without further ado, let's go straight to the code. This method avoids problems such as missing first letters, garbled strings, etc. It is very useful. Let's record it. The Ajax in the middle can directly call the login function to achieve automatic login Note: The input box needs to get the focus, which is necessary. Secondly, in order to prevent the carriage return from triggering manual login, you need to add the operation of onkeypress="if(event.keyCode==13) return false;". When the carriage return is pressed, the input box focus will not be lost unless the focus is manually switched or the focus() method is called. <div class="form-group"> <label for="inputUsernameEmail">Account</label> <input type="text" placeholder="Please enter your account" name="username" id="loginname" class="form-control" autofocus onkeypress="if(event.keyCode==13) return false;"> </div> Code: <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('Scan code completed') 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("System error"); }, 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('Scan the code to start...') code += e.key } if (lastCode && lastTime && nextTime - lastTime > 500) { // When there is a keypress event before scanning the code, prevent the first word from being missing console.log('Prevent the first word from being missing...') code = e.key } else if (lastCode && lastTime) { console.log('Scanning the code...') code += e.key } lastCode = nextCode lastTime = nextTime } } </script> The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
>>: Troubleshooting MySQL high CPU load issues
Easy installation of opencv2: conda install --cha...
Keepalived+Nginx+Tomcat to achieve high availabil...
This article shares with you the MySQL 8.0.13 ins...
1. Prepare the environment (download nodejs and s...
This article shares the specific code of the vue-...
Vue implements the palace grid rotation lottery (...
Docker basic instructions: Update Packages yum -y...
Table of contents 1. Main functions 2. Implementa...
The default program publishing path of tomcat7 is...
Table of contents Docker Installation Nvidia-dock...
Today I will introduce a very simple trick to con...
In order to speed up the parsing of the website, ...
Table of contents npm Install the loader Import P...
Alibaba Cloud purchases servers Purchase a cloud ...
1. Cause The requirement is to display two lines,...