首页 > 解决方案 > 防止条码扫描器进入下一个输入框

问题描述

我试图从数据库中找到一些值并使用以下代码使用条形码扫描仪添加到表中

<div class="form-group m-form__group">
    <label for="exampleInputEmail1">Search by Item name or barcode</label>
    <input type="text"  autofocus class="form-control m-input" id="productSearch"  placeholder="Item ">              
</div>

$( "#productSearch" ).change(function(event) {
    event.preventDefault();
  $.ajax({
          type: "get",
          context: this,
          url: "{!! asset('searchByProductName') !!}",
          dataType: 'json',
          data: { name:this.value },
          success: function(response)
            {
              if ($('#' + response.id).length !== 0)
              { $(this).val("").focus(); return false; }
             var markup = "<tr id="+response.id+"><input type='hidden' name='product_id[]'  value="+response.id+"><td><i class='flaticon-delete-1 delete-row' onclick='deleteRow(this)'></i></td><td>"+response.product_name+"</td><td>"+response.product_unit_price+"</td><td><input type='text' name='quantity[]' class='quantity' value='1'></td><td class='total'>"+response.product_unit_price+"</td><td>"+response.notes+"</td></tr>";

              $("table tbody").append(markup); 


                $(this).val("").focus(); return false; 
              } 
        });     
});

但问题是,一旦它从数据库中搜索它添加到表中的值,但指针会转到下一个输入框,因为我需要根据条形码扫描仪的检查不断地向表中添加值。我该如何防止呢?

标签: javascriptphpjqueryajaxbarcode-scanner

解决方案


我在比利时电话公司订阅系统网站上遇到过这样的问题,条形码扫描仪的末尾还会包含一个标签!

我用JS禁用了tab键!不记得我是否以某种方式将其限制为文本框中的文本(我想我做到了),但 JS 会是这样的(这都是来自内存顺便说一句):

$('#some-barcode-input').on('keyup', function(e){ 
    var code = e.keyCode || e.which;
    if(code == 9) { // 9 is the tab key
        return false;
    }
});

如果 keyup 不起作用,它可能是.change()或类似的。试试看!


推荐阅读