【问题标题】:Dynamically add row with jQuery使用 jQuery 动态添加行
【发布时间】:2020-04-13 13:15:09
【问题描述】:

我有一个与条形码扫描仪集成的应用程序,如下所示:

我已经用这段代码的 sn-p 动态地做了一行:

<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>

<div class="container" align="top" style="margin-top: 0px;">
    <h1 align="center">
        Barcode: <br><input type="text" id="myHeader" value="5555">
    </h1>
    <table id="myTable" class=" table order-list">
      <thead>
        <tr>
          <td>Barcode</td>
          <td>Item</td>
        </tr>
      </thead>
      <tbody>
      </tbody>
      <tfoot>
        <tr>
          <td colspan="7" style="text-align: left;">
            <div align="right">
              <p style="color: black;margin-right: 90px;">9.999.999</p>
            </div>
            <input type="button" class="btn btn-lg btn-block " id="addrow" value="Add Row" style="border-color: black;" />
          </td>
        </tr>
        <tr>
        </tr>
      </tfoot>
    </table>
</div>

这是我用脚本标签包装的代码:

<script >
    $(document).ready(function() {
        var counter = 0;

        $("#addrow").on("click", function() {
            var newRow = $("<tr>");
            var cols = "";

            cols += '<td><input type="text" disabled value="123123123" class="form-control" name="name' + counter + '"/></td>';
            cols += '<td><input type="text" disabled value="Sekop" class="form-control" name="mail' + counter + '"/></td>';

            cols += '<td><input type="button" class="ibtnDel btn btn-md btn-danger "  value="Delete"></td>';
            newRow.append(cols);
            $("table.order-list").append(newRow);
            counter++;
        });

        $("table.order-list").on("click", ".ibtnDel", function(event) {
            $(this).closest("tr").remove();
            counter -= 1
        });
    });

function calculateRow(row) {
    var price = +row.find('input[name^="price"]').val();
}

function calculateGrandTotal() {
    var grandTotal = 0;
    $("table.order-list").find('input[name^="price"]').each(function() {
        grandTotal += +$(this).val();
    });
    $("#grandtotal").text(grandTotal.toFixed(2));
} </script>

但上面的代码只是在我点击Add Row按钮时才添加行。

我现在想要的是,当我用条形码扫描仪扫描条形码时,自动添加的行会跟随扫描的条形码结果。 在这种情况下,它将是:“当标题上的条形码值发生变化时(&lt;h1 align="center"&gt;Barcode: 123123123&lt;/h1&gt;,当我单击 Add Row 按钮时结果相同。

所以,请参考任何方法、教程或示例代码如何做到这一点,我们将不胜感激:)

更多信息:这个应用程序的目的是为商店的收银员应用程序,例如当收银员扫描产品时,结果会自动出现在应用程序上。我使用 Python Flask 开发它。

【问题讨论】:

  • 首先检查条码扫描器启动时的事件。启动时,您需要触发add row 按钮,以便它会在您的表中添加一个新行。之后您可以找到表格的最后一行,然后可以替换BarcodeItem 的值。希望它可以帮助您最终确定您的方法。

标签: javascript jquery html barcode barcode-scanner


【解决方案1】:
$('#myTable tbody').append(newRow)

我认为你的 jQuery 选择器有问题。

试试上面显示的代码 - 希望它对你有用。

【讨论】:

    【解决方案2】:

    您必须聆听元素中所做的更改。你可以试试MutationObserver

    $(document).ready(function() {
      var counter = 0;
    
      $("#addrow").on("click", function() {
        var newRow = $("<tr>");
        var cols = "";
    
        cols += '<td><input type="text" disabled value="123123123" class="form-control" name="name' + counter + '"/></td>';
        cols += '<td><input type="text" disabled value="Sekop" class="form-control" name="mail' + counter + '"/></td>';
    
        cols += '<td><input type="button" class="ibtnDel btn btn-md btn-danger "  value="Delete"></td>';
        newRow.append(cols);
        $("table.order-list").append(newRow);
        counter++;
      });
    
      $("table.order-list").on("click", ".ibtnDel", function(event) {
        $(this).closest("tr").remove();
        counter -= 1
      });
    });
    
    function calculateRow(row) {
      var price = +row.find('input[name^="price"]').val();
    }
    
    function calculateGrandTotal() {
      var grandTotal = 0;
      $("table.order-list").find('input[name^="price"]').each(function() {
          grandTotal += +$(this).val();
      });
      $("#grandtotal").text(grandTotal.toFixed(2));
    }
    
    // Listen to the changes in the header element and add row
    var target = document.querySelector('#myHeader');
    
    setTimeout(function() {
      target.textContent = "New Barcode: XXXXXXXXX"; // change text after 2 seconds
    }, 2000)
    
    var observer = new MutationObserver(function(mutations) {
      mutations.forEach(function(mutation) {
        $("#addrow").trigger("click"); // trigger the click event to add row if the header text is changed
      });
    });
    
    var config = {
      attributes: true,
      childList: true,
      characterData: true
    };
    
    observer.observe(target, config);
    
    // otherwise
    observer.disconnect();
    observer.observe(target, config);
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
    <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
    
    <div class="container" align="top" style="margin-top: 0px;">
        <h1 align="center" id="myHeader">Barcode: 123123123</h1>
        <table id="myTable" class=" table order-list">
          <thead>
            <tr>
              <td>Barcode</td>
              <td>Item</td>
            </tr>
          </thead>
          <tbody>
          </tbody>
          <tfoot>
            <tr>
              <td colspan="7" style="text-align: left;">
                <div align="right">
                  <p style="color: black;margin-right: 90px;">9.999.999</p>
                </div>
                <input type="button" class="btn btn-lg btn-block " id="addrow" value="Add Row" style="border-color: black;" />
              </td>
            </tr>
            <tr>
            </tr>
          </tfoot>
        </table>
    </div>

    更新:更新的问题表明可以在条形码输入元素的模糊事件上触发点击事件:

    $(document).ready(function() {
      var counter = 0;
    
      $("#addrow").on("click", function() {
        var newRow = $("<tr>");
        var cols = "";
        var barcode = $("#myHeader").val().trim();// take the current barcode value
        cols += '<td><input type="text" disabled value= '+ barcode +'  class="form-control" name="name' + counter + '"/></td>';
        cols += '<td><input type="text" disabled value="Sekop" class="form-control" name="mail' + counter + '"/></td>';
    
        cols += '<td><input type="button" class="ibtnDel btn btn-md btn-danger "  value="Delete"></td>';
        newRow.append(cols);
        $("table.order-list").append(newRow);
        counter++;
      });
    
      $("table.order-list").on("click", ".ibtnDel", function(event) {
        $(this).closest("tr").remove();
        counter -= 1
      });
    });
    
    function calculateRow(row) {
      var price = +row.find('input[name^="price"]').val();
    }
    
    function calculateGrandTotal() {
      var grandTotal = 0;
      $("table.order-list").find('input[name^="price"]').each(function() {
          grandTotal += +$(this).val();
      });
      $("#grandtotal").text(grandTotal.toFixed(2));
    }
    
    // Add new row by triggering the click event on focus out
    var target = document.querySelector('#myHeader');
    target.addEventListener('blur', function(){
      $("#addrow").trigger("click");
    });
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
    <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
    
    <div class="container" align="top" style="margin-top: 0px;">
        Barcode: <br><input type="text" id="myHeader" value="5555">
        <table id="myTable" class=" table order-list">
          <thead>
            <tr>
              <td>Barcode</td>
              <td>Item</td>
            </tr>
          </thead>
          <tbody>
          </tbody>
          <tfoot>
            <tr>
              <td colspan="7" style="text-align: left;">
                <div align="right">
                  <p style="color: black;margin-right: 90px;">9.999.999</p>
                </div>
                <input type="button" class="btn btn-lg btn-block " id="addrow" value="Add Row" style="border-color: black;" />
              </td>
            </tr>
            <tr>
            </tr>
          </tfoot>
        </table>
    </div>

    【讨论】:

    • 您好,感谢您的回答,但它只是第一次工作(刷新浏览器后),如果我添加了新的条形码..?
    • @GofyandKitty,通过查看更新的问题,您似乎不需要突变观察者。您可以简单地在元素的焦点输出 (blur) 事件上触发 click 事件。我已经更新了答案,请检查:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 2011-10-17
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多