2013-04-30 25 views
8

Sto lavorando su scanner di codici a barre. Lo scanner di codici a barre che sto usando è un tipo plug-n-play e scansiona automaticamente il codice ovunque posizioni il cursore. Ma quello che voglio è che se posso eseguire la scansione ad una specifica casella di testo in una pagina web ogni volta che il mio scanner legge un codice aScansione codice a barre in una casella di testo specifica

Per esempio, se la mia forma assomiglia a questo

<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"> 

così ogni volta che scansionare un codice dovrebbe sempre apparire nella casella di testo txtitem, non importa dove sia il mio attuale focus.

Qualcuno può guidarmi o aiutarmi a trovare una soluzione qui ??

+0

E perché questo è collegato a PHP? Hai letto il manuale di questo scanner di codici a barre? Hai pensato di "come" lo scanner di codici a barre inserisce il codice nel tuo browser? Hai provato a usare javascript? – HamZa

+2

il mio male .. ho corretto i tag .. – Sriniwas

risposta

9

avete bisogno di ascoltare il caso "incolla" utilizzando jQuery

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

Ecco un esempio: http://jsfiddle.net/T6VdS/

+0

Per tutti quelli che non lo sanno. Gli scanner fungono da tastiera. Quando scandiscono il codice, lo passano sullo schermo. –

+0

non ho capito ... lo scanner invoca l'evento paste prima di inserire i dati ?? – Sriniwas

+0

Great ** + 1 **, e come @Tom Elmore ha detto, forse usare una regex per verificare se i dati incollati sono un codice a barre. – HamZa

0

penserei lo scanner è solo di essere visto come un dispositivo di input di testo come una tastiera ed emettere testo. A meno che non ci sia un modo per identificare quel testo, è probabile che la risposta sia che non esiste una soluzione facile.

Se il codice che si sta ricevendo è sempre nella stessa forma e può essere identificato con un'espressione regolare, si potrebbe essere in grado di spostarlo nella casella corretta in qualche modo bufferizzando l'input (mi aspetto che il codice scansionato venga inserito una serie di pressioni di tasti che sono molto più veloci di quelle che un umano dovrebbe inserire) e che esegue un'espressione regolare su di esso ...

0

Aggiungere un prefisso al testo che lo scanner emette (quasi tutto lo scanner consentirà di farlo) e quindi quando qualsiasi input inizia con quel prefisso sai che è lo scanner.

per catturare l'input con jQuery si potrebbe fare qualcosa di simile:

//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

alcuni scanner di codici a barre si comportano proprio come un altro dispositivo di input. Il modulo non può indicare la differenza tra le informazioni immesse da una tastiera o uno scanner a meno che non si utilizzi un timer per monitorare la velocità con cui viene immesso.

Alcuni scanner "incollano" i valori nel controllo focalizzato - altri inviano ogni singolo tratto di chiave.

Il seguente JSFiddle è in grado di rilevare quando si verifica ingresso quando i caratteri vengono inviati singolarmente su un singolo controllo:

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

Si potrebbe adattare questo per renderlo un delegato per l'intero modulo e rimuovere l'ingresso dal controllo in cui è stato inserito e inserirlo nella forma corretta.

il codice HTML di prova per il violino è questo:

<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> 

Il Javascript per violino campione è:

/* 
    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(); 

Il codice di cui sopra non supporta il copia/incolla, ma nella nostra situazione presente è improbabile che accada comunque.

+1

Non pensare che tu sia ancora nei paraggi, ma volevo ringraziarti per la tua meravigliosa risposta. Oggi dovevo fare i conti con gli input dei codici a barre e il tuo codice spiegava come prendere il controllo dell'input, anche più di quanto avrei fatto. Eccezionale! –

0

Il modo migliore è inserire i dati nel codice scansionato. Quasi tutti gli scanner supportano tale programmazione. Molti di questi possono essere programmati tramite codici a barre di controllo, che vengono stampati in manuale.

Uso Ctrl + Char per scanner Symbol, F9 dati F10 per scanner bluetooth Honeywel. Lo scanner Wasp non supporta la combinazione di caratteri Ctrl +. Quindi io uso il formato [Dati] per Wasp.

Quindi catturo il primo simbolo (ad esempio [char) nel programma e posiziona il cursore nella casella di ricerca. Dopo aver ricevuto l'ultimo carattere (nel mio caso) char) invia il contenuto della routine di ricerca.

Problemi correlati