【问题标题】:Autocomplete search with barcode scanner使用条形码扫描仪自动完成搜索
【发布时间】:2018-12-11 13:12:37
【问题描述】:

我已经实现了自动完成搜索,用户在其中输入产品名称并建议名称,通过单击名称,它会将其信息添加到现有表中。

但是对于条码扫描器,当它扫描产品时,产品名称会自动输入到输入框中。但是自动完成没有任何建议。

当条码扫描器扫描产品时,如何继续提供建议? 这是我用于自动完成搜索的代码。

查看:

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

JavasSript 部分:

$('#productSearch').autocomplete({
  source: '{!! asset('productSearch') !!}',
  select:function(suggestion,ui) {    
    var markup = "<tr id="+ui.item.id+"><input type='hidden' name='product_id[]'  value="+ui.item.id+"><td><i class='flaticon-delete-1 delete-row' onclick='deleteRow(this)'></i></td><td>"+ui.item.value+"</td><td>"+ui.item.unit_price+"</td><td><input type='text' name='quantity[]' class='quantity' value='1'></td><td class='total'>"+ui.item.unit_price+"</td><td>"+ui.item.notes+"</td> </tr>";              
    $("table tbody").append(markup);          
  }
})

【问题讨论】:

    标签: javascript php autocomplete jquery-ui-autocomplete barcode-scanner


    【解决方案1】:

    条形码不会触发任何关键事件,因此您可以执行以下操作:

    $('#my_field').on({
        keypress: function() { typed_into = true; },
        change: function() {
            if (typed_into) {
                alert('type');
                typed_into = false; //reset type listener
            } else {
                alert('not type');
            }
        }
    });
    

    根据您想要评估的时间,您可能希望在提交时而不是在更改时进行检查。

    【讨论】:

      【解决方案2】:

      keydown 事件上触发自动完成,因此如果您的输入值不为空(由条形码扫描仪更新),您可以触发它:

      if (!$('#productSearch').val()){
          $('#productSearch').trigger('keydown');
      }
      

      【讨论】:

      • 你能建议用代码吗?我不明白我是如何使用现有代码实现的
      • @User57 更新的答案尝试将此添加到您的 JavaScript。也许有更好的方法,但您需要提供更多代码。条形码扫描仪读取数据时如何将数据输入输入?
      猜你喜欢
      • 2020-10-22
      • 1970-01-01
      • 1970-01-01
      • 2012-08-08
      • 2020-11-18
      • 1970-01-01
      • 1970-01-01
      • 2012-05-21
      • 1970-01-01
      相关资源
      最近更新 更多