每次发生新扫描时刷新输入框。 jQuery - Scannerdetection api

时间:2016-03-03 06:29:16

标签: javascript jquery barcode-scanner

我想使用jQuery扫描程序检测api开发一个简单的Web应用程序。我已经按照给定的两个链接中的教程进行了操作。

jQuery.ScannerDetection - tutorial & best practices

Detect when input box filled by keyboard and when by barcode scanner.

这是我的代码

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="./scanner_detection/jquery.scannerdetection.js"></script>
    <script type="text/javascript">
        $(document).scannerDetection({
            timeBeforeScanTest: 200, // wait for the next character for upto 200ms
            startChar: [120], // Prefix character for the cabled scanner (OPL6845R)
            endChar: [13], // be sure the scan is complete if key 13 (enter) is detected
            avgTimeByChar: 40, // it's not a barcode if a character takes longer than 40ms
            onComplete: function(barcode, qty){ ... } // main callback function 
        });
    </script>
</head>
<body>
    Barcode<br>
    <input id="barcode" type="text" value="Barcode appears here">
    <script>
        $(window).ready(function(){
            console.log('all is well');
            $(window).scannerDetection();
            $(window).bind('scannerDetectionComplete',function(e,data){
                console.log('complete '+data.string);
                $("#barcode").val(data.string);
            })
                .bind('scannerDetectionError',function(e,data){
                console.log('detection error '+data.string);
            })
                .bind('scannerDetectionReceive',function(e,data){
                console.log('Recieve');
                console.log(data.evt.which);
            })
    </script>
</body>
</html>

当我扫描条形码时,条形码值出现在输入框中,但是当第二次扫描发生时,第二个条形码值附加到前一个条形码值。每次新扫描发生时如何清除以前的条形码?

当我扫描两个条形码时,输入框看起来像这样。

enter image description here

每次新扫描时,如何刷新文本框?

1 个答案:

答案 0 :(得分:0)

也许你可以尝试这样的事情。

$(window).bind('scannerDetectionComplete',function(e,data){
    console.log('complete '+data.string);
    $("#barcode").removeAttr('value');
    $("#barcode").val(data.string);
})

希望这有帮助。