Clear field type search

Asked

Viewed 32 times

-1

I’m having a problem that I can’t solve, I’m building a library inventory system, and I’m using a reader to read the barcodes from the books, but after I use the reader to pick up the code and search the bank, the field is not clean, but when I add the code $(document).keypress(function(e) { if(e.which == 13) $('#btn-search').click(); document.getElementById('input-search').value=''; }); The field is clean, but as the reader of an enter after reading, the search parameter goes blank and then the system makes a query in the database with empty parameter, follows the code.

Search.php `

search($search) Book not registered in the database... Search data:
  <table class="table table-striped table-bordered">
     <thead class="thead-dark">
        <tr>
           <th>Titulo</th>
           <th>Codigo de Barras</th>
           <th>Biblioteca</th>
        </tr>
     </thead>
     <tbody>
        <?php foreach($user->search($search) as $row): ?>
        <tr>
           <td><?php echo $row->tituloexemplar; ?></td>
           <td><?php echo $row->codigobarra;?></td>
           <td><?php echo $row->nomebiblioteca; ?></td>
        </tr>
        <?php endforeach; ?>
     </tbody>
  </table>
` php search. ` Inventory Research
  <!-- script -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
  <script src="js/usuarios.js"></script>
  <script>
     $(document).keypress(function(e) {
        if(e.which == 13) $('#btn-search').click();
        document.getElementById('input-search').value='';
  });
  </script>
`

1 answer

0

Try this.

<!DOCTYPE html>
<html lang="">
<body>
<input id="input-search" placeholder="informe o código"/> <br/> <br/> <input id="input-display"/>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script>
    $('#input-search').keypress(function (e) {
        
        let value_search;
        

        // se precionar enter numerico ou alfanumerico  e input não for vázio
        // (leitores emulam "Ender" do teclado)
        if (e.key == 'Enter' && !!$(this).val()) {
            
            // guardar valor do input
            value_search = $(this).val();
            
            // limpar input
            $(this).val('');
            
            //  agora o valor está separado do input que foi limpo para próxima consultar
            // aqui poderá ser trabalhado a lógia par o valor informado.
            $('#input-display').val(value_search)
            
        }
    });

</script>
</body>
</html>

  • Opa Lindomar, thank you for the answer, but still fall into the same dilemma, the input-search field, after reading in the reader he does not clean the field, then when I try to read another book, the field remains filled, I would need that every time a query is made in the bank, the input-search field was cleared, but without executing an action, until you read the next code.

  • change Document.getElementById('input-search'). value=''; by $('#input-search'). val('')

  • Tested the code? It clears the input-search field after the query, just needs to adapt to your need

  • I did the test, but when I do the process it fills in the second input, and in the first it looks for all the bank results, maybe the problem is my javascript, but I could not identify what is wrong.

  • $(Document). ready(Function() { // Search the database while holding down the $("#btn-search").click(Function() { var search = $("#input-display").val(); search(search); }); Function search(value) { $.ajax({ type: 'POST', dataType: 'html', url: 'fetch.php', beforeSend: Function() { $("#data").html("Loading..."); }, date: {value: value}, Success: Function (result) { $("#data").html(result); } }); } });

  • Lindomar, I made a change here in js and it worked, thank you very much.

Show 1 more comment

Browser other questions tagged

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