使用jQuery动态添加行

问题描述 投票:1回答:2

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

enter image description here

我已经使用此代码段动态地进行了一行:

<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>

但是上面的代码仅在单击添加行按钮时才添加行。

我现在想要的是,当我使用条形码扫描仪扫描条形码时,会自动在扫描的条形码结果之后添加该行。在这种情况下,将是:“当标题上的条形码值更改时[<h1 align="center">Barcode: 123123123</h1>,当我单击添加行按钮时,结果是相同的。

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

[有关其他信息:此应用程序的用途是商店上的收银员应用程序,就像收银员扫描产品时一样,结果会自动显示在该应用程序上。然后使用Python Flask开发它。

javascript jquery html barcode barcode-scanner
2个回答
1
投票
$('#myTable tbody').append(newRow)

我认为您的jqury选择器有问题尝试上面的代码希望它可以工作


1
投票

您必须听取元素中所做的更改。您可以尝试使用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>
© www.soinside.com 2019 - 2024. All rights reserved.