JQuery trying to take the focus out of the text input fields by a scan

375 views Asked by At

I'm working on a small project, and after a long documentation with no result I decided to ask some help. What I'm working on is an allocation barcode based system in php and jquery. The allocations process as you will see in the code, includes four scans, first scan to open the allocation session, second to scan the order, third to scan the location shelf and fourth to get the confirmation window by rescanning the order(step where the bug is). The last scan supposed to blur out all the input fields of the form before opening the confirmation window. This works for a while, but then the code start to keep the focus on the last selected input field, messing up the numbers by this.

This is the scanner detection function having as callback function the four steps to follow scan by scan.(it is wrapped in a document.ready function , I just didn't copy the whole code)

   $(document).scannerDetection({
   timeBeforeScanTest: 200, // wait for the next character for upto 200ms
   startChar: [120], // Prefix character for the cabled scanner (OPL6845R)
   endChar: [13], // be sure the scan is complete if key 13 (enter) is 
   detected
   avgTimeByChar: 40, // it's not a barcode if a character takes longer than 
   40ms
   onComplete: function(barcode, qty){
    // main callback function
      console.log(barcode);
      order=barcode;
        if($("*:contains('ADD YOUR ORDER')").length>0){
          count++;
          if(count==1){
          console.log('First step');
          $("#allocation #ordNum").focus();
          var lineOrd=$("#allocation #ordNum").closest("tr");
          $(lineOrd).addClass("focus");

        }
          if(count==2){
            console.log('Second Step');
          if($("#allocation #ordNum").val()) {
            var matchItem=/^\d+$/;
            ord=$("#allocation #ordNum").val();
            if(matchItem.test(ord)===false){
              alert("Give a valid order number!");
              window.location.href=window.location.href;
            }
            $(".focus").removeClass("focus");
            $.post("ajax.inc.php",
            {action:"scanN",order:ord}).done(function(data){

            dataa=$(data).text();
            if(dataa.indexOf("1.)")>=0){
            document.getElementById("soundEffect").play();
            alerting=true;
            alert(dataa);
          }
            });
            console.log("there is something: "+ord);
            $("#allocation #location").focus();
            var lineLoc=$("#allocation #location").closest("tr");
            $(lineLoc).addClass("focus");

          }else{
            alert('Give a valid ordernumber.');
            count--;
          }
        }

              if(count==3){
                console.log('Third step');
                if($("#allocation #location").val()){
                  loc=$("#allocation #location").val();
                  $(":text").blur(); //the line what stops working
                  console.log('location has value');
                  $("#allocation #update").focus();
                  //var matchItem2=/^\s?[A-B]\s?[1-6]\s?[A-E]\s?[1-6]\s?$/;
                  var matchItem2=/^\s?[A-B]\s?[1-6]\s?[A-E]\s?[1-6]\s*$/;
                  if(matchItem2.test(loc)===false){
                    alert("Give a valid location!");
                    window.location.href=window.location.href;
                  }

                }
              }


                if(count==4){
                  console.log('Forth step');
                  $(".focus").removeClass("focus");
                if(barcode===ord){
                  var confi=confirm('Order '+ord+' added to location '+loc);
                  if(confi==true){
                    $.post("ajax.inc.php",
    {action:"scan",order:ord,location:loc,thestat:thestat})
    .done(function(data){
                      console.log('Order added');
                      $(".container").append(data);
                        setTimeout(function(){
                          window.location.href=window.location.href;
                        },500);
                    //window.location.href=window.location.href;
                    console.log('confirmed');
                  });
                }
                  if(confi==false){
                    window.location.href=window.location.href;
                    console.log('not confirmed');
                  }

                }else{
                  alert('Allocation is confirmed by rescan of the order');
                  count--;
                }

              }

    }


        if($("h2.scan:contains('SCAN TO SEARCH ORDER')").length>0){
          $(".container #locations").remove();
          $(".container h4#process").remove();
          console.log('Scanning Picknote');

       $.post("ajax.inc.php{action:"scanN",order:order}).done(function(data)
         {
            $(".container").append(data);
          });
      }
      if($("h2.scan:contains('SCAN TO PROCESS ORDER')").length>0){
            console.log('Delete order');
            console.log('order:'+order);
                $.post("ajax.inc.php,
                {action:"delete",order:order}).done(function(data){
                    $(".container #processed").append(data);
                    $(".container #locations .forProcess 
                span#"+order).slideUp();
                });
             }

             }
               });

Any help can be helpful.What I want to achieve is, once the scan is at number 3, to focus out of every input field on the form.Even if a achieved it, for some reason it stopped doing it after a while. Thank you.

1

There are 1 answers

2
Colin On

Please check this line:

$.post("ajax.inc.php{action:"scanN",order:order})

I think it should read:

$.post("ajax.inc.php",{action:"scanN",order:order})