Home > Mobile >  Ajax trigger keyup function changing to instant trigger
Ajax trigger keyup function changing to instant trigger

Time:10-21

i'm trying to get details information from database according to input from id_card . im already succeed getting the information after I'm typing the id_card in the input box by using keyup function. What i need to do now is I want result appear without I have to press any key after input was trigger. As information, the input will fill in by using QR scanner.

My HTML

<input type="text" id="id_card" placeholder="Capture by QR Code Scanner" name="id_card">
<input type="text" id="fullname" placeholder="Staf Name" name="fullname" readonly>

My Ajax

$(document).ready(function() {
  $('#id_card').keyup(function() {
    var id_card = $(this).val();
    $.ajax({
      url: "<?php echo base_url('index.php/scan/get_details'); ?>",
      method: 'post',
      data: {id_card: id_card},
      dataType: 'json',
      success: function(response) {
        var len = response.length;

        document.getElementById("fullname").value = "";

        if (len > 0) {
          $("#id_card").load();
          var uname = response[0].id_card;
          var name = response[0].fullname;

          document.getElementById("fullname").value = response[0].fullname;
        }
      }
    });
  });
});

I'm trying to change keyup function to onchange but no luck.

Thank you for helping.

CodePudding user response:

Try

$(function() {
   $('#id_card').on("input", function(e) { // pasted or typed
   let id_card = $(this).val();
   if (id_card.replace(/\D /g).length < 16) return 
   // here the ajax
  • Related