将条形码扫描到特定的文本框中

时间:2013-04-30 09:19:18

标签: javascript barcode barcode-scanner

我正在研究条形码扫描仪。我使用的条形码扫描器是一种即插即用类型,无论您放置光标的位置,都会自动扫描代码。但我想要的是,每当我的扫描仪读取代码时,我是否可以将其扫描到网页上的特定文本框

例如,如果我的表格看起来像这样

<input type="text" name="txtItem" id="txtItem" class="m-wrap w-120" tabindex="6">

<input type="text" name="itemId" id="itemId" class="m-wrap w-120" tabindex="6">

<input type="text" name="itemName" id="itemName" class="m-wrap w-120" tabindex="6">

<input type="text" name="itemQty" id="itemQty" class="m-wrap w-120" tabindex="6">

因此每次扫描代码时,无论我目前的焦点在哪里,它都应始终显示在txtitem文本框中。

有人可以指导我或帮我找到解决方案吗?

5 个答案:

答案 0 :(得分:19)

某些条形码扫描仪的作用与其他输入设备相同。除非您使用计时器监控输入的速度,否则表单无法区分键盘输入的信息与扫描仪之间的区别。

有些扫描仪会将值“粘贴”到聚焦控件中 - 其他扫描程序会发送每个单独的击键。

以下JSFiddle能够检测何时在单个控件上单独发送字符时发生输入:

http://jsfiddle.net/PhilM/Bf89R/3/

您可以对此进行调整以使其成为整个表单的委托,并从输入的控件中删除输入并将其放入正确的表单中。

小提琴的测试html是这样的:

<form>
    <input id="scanInput" />
    <button id="reset">Reset</button>
</form>
<br/>
<div>
    <h2>Event Information</h2>
    Start: <span id="startTime"></span> 
    <br/>First Key: <span id="firstKey"></span> 
    <br/>Last Ley: <span id="lastKey"></span> 
    <br/>End: <span id="endTime"></span> 
    <br/>Elapsed: <span id="totalTime"></span>
</div>
<div>
    <h2>Results</h2>
    <div id="resultsList"></div>
</div>

样本小提琴的Javascript是:

/*
    This code will determine when a code has been either entered manually or
    entered using a scanner.
    It assumes that a code has finished being entered when one of the following
    events occurs:
        • The enter key (keycode 13) is input
        • The input has a minumum length of text and loses focus
        • Input stops after being entered very fast (assumed to be a scanner)
*/

var inputStart, inputStop, firstKey, lastKey, timing, userFinishedEntering;
var minChars = 3;

// handle a key value being entered by either keyboard or scanner
$("#scanInput").keypress(function (e) {
    // restart the timer
    if (timing) {
        clearTimeout(timing);
    }

    // handle the key event
    if (e.which == 13) {
        // Enter key was entered

        // don't submit the form
        e.preventDefault();

        // has the user finished entering manually?
        if ($("#scanInput").val().length >= minChars){
            userFinishedEntering = true; // incase the user pressed the enter key
            inputComplete();
        }
    }
    else {
        // some other key value was entered

        // could be the last character
        inputStop = performance.now();
        lastKey = e.which;

        // don't assume it's finished just yet
        userFinishedEntering = false;

        // is this the first character?
        if (!inputStart) {
            firstKey = e.which;
            inputStart = inputStop;

            // watch for a loss of focus
            $("body").on("blur", "#scanInput", inputBlur);
        }

        // start the timer again
        timing = setTimeout(inputTimeoutHandler, 500);
    }
});

// Assume that a loss of focus means the value has finished being entered
function inputBlur(){
    clearTimeout(timing);
    if ($("#scanInput").val().length >= minChars){
        userFinishedEntering = true;
        inputComplete();
    }
};


// reset the page
$("#reset").click(function (e) {
    e.preventDefault();
    resetValues();
});

function resetValues() {
    // clear the variables
    inputStart = null;
    inputStop = null;
    firstKey = null;
    lastKey = null;
    // clear the results
    inputComplete();
}

// Assume that it is from the scanner if it was entered really fast
function isScannerInput() {
    return (((inputStop - inputStart) / $("#scanInput").val().length) < 15);
}

// Determine if the user is just typing slowly
function isUserFinishedEntering(){
    return !isScannerInput() && userFinishedEntering;
}

function inputTimeoutHandler(){
    // stop listening for a timer event
    clearTimeout(timing);
    // if the value is being entered manually and hasn't finished being entered
    if (!isUserFinishedEntering() || $("#scanInput").val().length < 3) {
        // keep waiting for input
        return;
    }
    else{
        reportValues();
    }
}

// here we decide what to do now that we know a value has been completely entered
function inputComplete(){
    // stop listening for the input to lose focus
    $("body").off("blur", "#scanInput", inputBlur);
    // report the results
    reportValues();
}

function reportValues() {
    // update the metrics
    $("#startTime").text(inputStart == null ? "" : inputStart);
    $("#firstKey").text(firstKey == null ? "" : firstKey);
    $("#endTime").text(inputStop == null ? "" : inputStop);
    $("#lastKey").text(lastKey == null ? "" : lastKey);
    $("#totalTime").text(inputStart == null ? "" : (inputStop - inputStart) + " milliseconds");
    if (!inputStart) {
        // clear the results
        $("#resultsList").html("");
        $("#scanInput").focus().select();
    } else {
        // prepend another result item
        var inputMethod = isScannerInput() ? "Scanner" : "Keyboard";
        $("#resultsList").prepend("<div class='resultItem " + inputMethod + "'>" +
            "<span>Value: " + $("#scanInput").val() + "<br/>" +
            "<span>ms/char: " + ((inputStop - inputStart) / $("#scanInput").val().length) + "</span></br>" +
            "<span>InputMethod: <strong>" + inputMethod + "</strong></span></br>" +
            "</span></div></br>");
        $("#scanInput").focus().select();
        inputStart = null;
    }
}

$("#scanInput").focus();

上面的代码不支持复制/粘贴,但在我们的情况下,这种情况不太可能发生。

答案 1 :(得分:11)

您需要使用jQuery

监听“粘贴”事件
$("input").on("paste",function(e){
    $("#txtItem").focus();
});

这是一个例子: http://jsfiddle.net/T6VdS/

答案 2 :(得分:0)

我认为扫描仪只是被视为文本输入设备,如键盘和输出文本。除非有办法识别该文本,否则答案可能是没有一个简单的解决方案。

如果您收到的代码始终采用相同的形式并且可以使用正则表达式进行识别,您可以通过某种方式缓冲输入将其移动到正确的框中(我希望扫描的代码能够进入一系列比人类输入要快得多的按键,并在它上面运行正则表达式......

答案 3 :(得分:0)

为扫描仪输出的文本添加前缀(几乎所有扫描仪都允许您这样做),然后当任何输入以该前缀开头时,您就知道它是扫描仪。

要使用jquery捕获输入,可能执行以下操作:

//presuming the scanner acts like a keyboard
$(document).keypress(function (e) { 

    //do something to match the 'key presses' 

    //focus to the input and put the rest of the string in there

}); 

答案 4 :(得分:0)

最好的方法是放 数据到扫描的代码。几乎所有扫描仪都支持这种编程。其中许多都可以通过手动打印的控制条形码进行编程。

我使用Ctrl + Char for Symbol扫描仪, 适用于Honeywel蓝牙扫描仪的F9数据F10。 黄蜂扫描仪不支持Ctrl +字符组合。所以我用 黄蜂的[数据]格式。

然后我抓住第一个符号(在节目中说[char],将光标放在搜索框中。收到最后一个字符(在我的情况下)char)将内容发送到搜索例程。

相关问题