Jquery monitoring key sequences

Asked

Viewed 512 times

4

I’m making a form, where data is entered by scanning barcodes (default code 128).

After scanning each value, the handheld scanner is already set to give a <TAB> and go to the next field, making it very practical in the repetitive use of this form.

But the ENTER has to be via keyboard. Losing that sequence of agile registrations. So I thought about creating a barcode, with a sequence of values in which through a javascript function I would understand that that is a sign of ENTER, ex: In the code it would be '+++++ Jquery would understand this sequence and would make the form Submit.

But unfortunately it didn’t work, because Jquery in my function can only understand the first key and not the sequence.

$(document).bind('keydown', '+++++', function (e) {
    alerte('teste');
    $('form#myForm').submit();
});

Or is there a way to make a special barcode for shipping?

Example in Fiddle https://fiddle.jshell.net/tvdpL6xe/

  • I don’t understand what you tried to do, if you will create a bar code, don’t just check it and run the function ENTER based on that?

  • I tried to make a barcode with the +++++ sequence or any other character.. and then jquery would understand Keydown and trigger a Submit.. I’ll try to edit to make it clearer

  • Not enough after . Blur() gives a Trigger('click') on target element ?

  • Just a curiosity: when the scanner reads the bars and throws the numbers into the input, what event does the script detect (change, keydown, keyup....)? Obg!

  • then the barcode scanner, works like a usb keyboard.. vc scans the code and it plays the string in the field with focus.. being the same keyboard process, keydown, keyup. etc

3 answers

4

Another way to solve your problem without having to set up a special code would be to check that all inputs from form are filled and if yes, do the Submit.

$(document).bind('focusout', 'input', function (e) { // Podemos verificar no blur por que o leitor está configurado para dara um <TAB> ap
  var naoPreenchidos = $('form').find('input[type="text"]').filter(function() {
    return this.value === '';
  });
  if (naoPreenchidos.length === 0) {
    alert('teste vai postar');
    $('form#myForm').submit();
  }
});

Ma if you prefer to check the code you can check the sequence in the event of keyup instead of keydown, this way you can access the input value and compare with the code you configure.

$(document).bind('keyup', function (e) {
  var codigo = $(e.target).val();
  if (codigo === '+++++') {
    alert('teste vai postar');
    $('form#myForm').submit();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="teste" method="post">
  <input type="text" id="campo1" /><br>
  <input type="text" id="campo2" /><br>
  <input type="text" id="campo3" /><br>
  <input type="submit" value="enviar" />
</form>

  • the code worked, but when I put it in my form it doesn’t do Submit and gives an error in my jquery.validation jquery.validate.js:786 Uncaught Typeerror: Globalize.parseNumber is Since if I clicked the Submit button it would send or validate the form

  • You tried what solution?

4


You can create a global variable to go concatenating and comparing, when it is equal you run and clean, when there is a character other than + you also clean, follows below an example:

$(document).ready(function () {
  var codigo = "";
  $(document).bind('keypress', function (e) {
    if (e.charCode === 43) {
      for (var i = 0; i < codigo.length; i++) {
        if (codigo.substr(i, 1) != '+') {
          codigo = "";
          break;
        }
      }

      codigo = codigo + '+';

      if (codigo == '+++++') {
        alert("Vai enviar!");
        $('#teste').submit();
        codigo = "";
      }
    } else {
      codigo = "";
    }
  });
});
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<form id='teste' method='post'>
  <input type='text' id='campo1'><br>
  <input type='text' id='campo2'><br>
  <input type='text' id='campo3'><br>
  <input type='submit' value='enviar'>
</form>

For the event keydown was not giving to check which key was pressed, in the event keypress gave just right.

  • Apparently it didn’t work, first I saw that he enters that if do for even keys not equal to +, and then even typing+++++

  • The for only checks whether the variable codigo contains some character other than +, if there is, it clears the variable.

  • https://fiddle.jshell.net/tvdpL6xe/

  • Doing the tests on JSFiddle I realized that when we executed "++".substring(1, 1) it returns empty, so was entering the if. But if we execute "++".substr(1, 1) it returns "+", then with substr instead of substring the code will work. I will change in the reply too.

  • now I think it’s perfect!

  • when entering any data it says it will send

  • I made two changes, the first was to change keydown for keypress and the second was to put a if before executing the code.

Show 2 more comments

3

After scanning each value, the handheld scanner is already set to give a <TAB> and go to the next field, making it very practical in the repetitive use of this form.

Then I believe that the solution would be as already said in the comments see.

Suppose the last field has the attribute ID defined as codigo, it is the last to be filled and the last to be given TAB, so he’s the last to lose focus.

// Quando o input[id='codigo'] perde o foco
$("#codigo").on('blur', function(e) {
    var valor = $(this).val();
    // Verifica os cinco últimos caracteres
    if (valor.slice(-5) === '+++++') {
        // Se é igual executa uma ação
        $("form").submit();
    }
});

See working

$("#codigo").on('blur', function(e) {
  var valor = $(this).val();
  // Verifica os cinco últimos caracteres
  if (valor.slice(-5) === '+++++') {
    // Se é igual executa uma ação
    console.log('ENVIAR FORMULARIO');
    console.log('+------------------------------+');
    console.log('PRODUTO: '+$("#nome").val());
    console.log('VALOR  : '+$("#valor").val());
    console.log('CÓDIGO : '+$("#codigo").val());
    console.log('+------------------------------+');
    $("input[type='text']").val('');
    // $("form").submit();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="teste" method="post">
  <div>Nome</div>
  <input type="text" id="nome" />
  <div>Valor</div>
  <input type="text" id="valor" />
  <div>Código</div>
  <input type="text" id="codigo" />
  <input type="button" value="enviar" id="btnEnviar" />
</form>

Reference

Browser other questions tagged

You are not signed in. Login or sign up in order to post.