Typeahead JS 无法处理具有相同 类 的多个输入

Typehead JS is not working on multiple inputs with same classes

我遇到的问题与发票系统有关。默认情况下存在一行,但如果有不止一种产品,则用户可以 添加新项目 ,它使用 jQuery 追加功能添加新行。

发票系统的输入很少,其中之一是由 Typehead JS 自动完成的项目名称。

预定义行代码

<tbody id="TableBody">
  <tr>
    <td style="width: 220px">
      <input type="text" class="form-control Item" name="Item" id="Item" placeholder="Nombre del producto" required autocomplete="off">
    </td>
    <td>
      <input type="number" name="QTV" min="1" name="QTV" id="QTV" class="form-control text-right" placeholder="00" required>
    </td>
    <td>
      <input step="2" type="number" class="form-control text-right" min="1" id="Rate" placeholder="0.00" readonly>
    </td>
    <td>
      <input step="any" id="Disc" name="Disc" type="number" min="0" name="" class="form-control text-right" placeholder="00">
    </td>
    <td>
      <input type="text" name="SubTotal" class="form-control text-right" id="Total" placeholder="Total" readonly>
    </td>
    <td>
      <button type="button" class="btn btn-danger DelRow">Delete</button>
    </td>
  </tr>
</tbody>

添加新行的代码

$('#AddNewItem').click(function() { $('#TableBody').append(`
<tr>
  <td style='width: 220px'>
    <input type='text' class='form-control Item' name='Item' id='Item' placeholder='Nombre del producto' required autocomplete='off'>
  </td>
  <td>
    <input type='number' name='QTV' min='1' name='QTV' id='QTV' class='form-control text-right' placeholder='00' required>
  </td>
  <td>
    <input step='2' type='number' class='form-control text-right' min='1' id='Rate' placeholder='0.00' readonly>
  </td>
  <td>
    <input step='any' id='Disc' name='Disc' type='number' min='0' name='' class='form-control text-right' placeholder='00'>
  </td>
  <td>
    <input type='text' name='SubTotal' class='form-control text-right' id='Total' placeholder='Total' readonly>
  </td>
  <td>
    <button type="button" class="btn btn-danger DelRow">Delete</button>
  </td>
</tr>
`); });

显示自动建议的代码

$(document).ready(function() {
 $('.Item').typeahead({
  source:  function (query, process) {
  return $.get('FetchItem.php?Item', { query: query }, function (data) {    
    data = $.parseJSON(data);
    return process(data);
   });
  },
 });
});

我只想显示有关所有附加附加字段的建议。 任何帮助将不胜感激,谢谢

Screenshot

毕竟,我得到了答案的解决方案。

我正在使用 jQuery 文档加载功能,但它无法处理附加的行。

所以我没有使用 $(document).ready(function() { 而只是使用了 $('.TableBody').on("click", ".Item", function() { 并且它起作用了。