avoid Ubmit of a form with barcode reader

Asked

Viewed 2,080 times

2

Well I have the following form:

<form name="produto" method="post" action="cadastra.php">
   <input name="cod" type='text'>
   <button type='submit'>FINALIZAR</button>
</form>

Good Whenever I use the barcode reader it reads the code and submits the form, how do I avoid that? I just want it to read the code.

Is there any way to do this with jQuery?

  • You can take out the Ubmit and when you want to click the button to submit, then submit.

3 answers

4


Usually the barcode reader sends a enter after reading, you can configure the reader not to perform enter, or you can detect action in the field and prevent the Submit action from being called using the method Event.preventDefault()

$('#cod').on('keypress',function(event){
  //Tecla 13 = Enter
  if(event.which == 13) {
    //cancela a ação padrão
    event.preventDefault();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="produto" method="post" action="cadastra.php">
   <input name="cod" type='text' id="cod">
   <button type='submit'>FINALIZAR</button>
</form>

Note that clicking the field and pressing enter does not send the form but clicking the yes button.

1

The barcode reader works like a keyboard. It identifies the code, type and press enter. vc have to configure the reader and disable the auto enter function.

  • 2

    This is an idea, but it does not answer the question, which asks a suggestion in the client, jQuery for example.

0

Use e.preventDefault(); According to the Jquery documentation; "If this method is called, the default event action will not be triggered."

Source: https://api.jquery.com/event.preventdefault/

Example:

$('#meu_form').submit(function(e) { 
            e.preventDefault();
}); 
  • 1

    But that won’t stop the form be sent, even when desired?

  • You are right. In case it is do as mentioned. Treat if the key between was triggered, otherwise send the form.

Browser other questions tagged

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