2013-04-30 101 views
8

我正在研究條形碼掃描儀。我正在使用的條形碼掃描器是即插即用類型,無論您放置光標的位置如何,都會自動掃描代碼。但我想要的是,我是否可以將其掃描到一個特定的文本框上的網頁,每次我的掃描器讀取代碼將條形碼掃描到特定的文本框中

對於例如,如果我的形式看起來像這樣

<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文本框中,無論我當前的重點在哪裏。

任何人都可以指導我或幫我找到解決方案?

+0

爲什麼這與PHP相關?您是否閱讀過該條形碼掃描儀的手冊?你有沒有想過條形碼掃描器是如何將代碼放入瀏覽器的?你有沒有嘗試過使用JavaScript? – HamZa 2013-04-30 09:28:33

+2

我的壞..我糾正了標籤.. – Sriniwas 2013-04-30 09:30:26

回答

9

你要聽上使用jQuery

$("input").on("paste",function(e){ 
    $("#txtItem").focus(); 
}); 

這裏的「膏」事件就是一個例子: http://jsfiddle.net/T6VdS/

+0

給大家誰不知道。掃描儀充當鍵盤。當他們掃描代碼時,他們將其掃描到屏幕上。 – 2013-04-30 09:33:29

+0

我沒有得到你..掃描儀在輸入數據之前調用粘貼事件嗎? – Sriniwas 2013-04-30 09:34:07

+0

Great ** + 1 **,和@Tom Elmore說的一樣,也許使用正則表達式來檢查粘貼的數據是否是條形碼。 – HamZa 2013-04-30 09:34:52

0

我認爲掃描儀是剛剛被視爲像鍵盤文本輸入設備並輸出文字。除非有方法來識別文本,否則答案很可能是沒有簡單的解決方案。

如果您收到的代碼總是以相同的形式出現,並且可以通過正則表達式進行標識,那麼您可以通過某種方式緩衝輸入來將其移動到正確的框中(我希望掃描的代碼進入一系列比人類快得多的按鍵輸入)並在其上運行正則表達式...

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 

}); 
17

一些條碼掃描器行爲就像其他輸入設備。除非您使用計時器來監視輸入的速度,否則表單無法區分鍵盤輸入的信息和掃描儀輸入的信息之間的差異。

一些掃描儀將值粘貼到聚焦控件中 - 其他人發送每個單獨的擊鍵。

以下的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

不要以爲你還在身邊,但我想感謝你的美妙回答。我今天不得不處理條形碼輸入,你的代碼涵蓋了如何控制輸入,甚至比我要做的還要多。優秀! – 2017-07-14 16:51:24

0

最好的方法是將 數據放入掃描後的代碼中。幾乎所有的掃描儀都支持這種編程。它們中的許多都可以通過控制條碼進行編程,這些都是手動打印的。

我使用Ctrl + Char作爲Symbol掃描儀, F9數據F10用於Honeywel藍牙掃描儀。 黃蜂掃描器不支持Ctrl +字符組合。所以我用Wasp的 [Data]格式。

然後我抓住第一個符號(說[char]在程序中的位置光標在搜索框中。在收到最後一個字符(在我的情況下)字符)發送到搜索例程的內容。