javascript Dynami c Несколько входов - PullRequest
0 голосов
/ 25 февраля 2020

почему не работает этот код? Может кто знает?

Таблица:

$(document).ready(function() {
  var postURL = "<?php echo url('invoices/create') ?>";
  var i = 1;
  // Add inputs
  $('#add').click(function() {
    i++;
    $('#dynamic_field').append('' +
      '<tr id="row' + i + '">' +
      '<td><input class="form-control form-control-alternative" type="text" name="description[]" placeholder="Description"></td>' +
      '<td><input class="form-control form-control-alternative" type="text" name="qty[]" placeholder="Qty"></td>' +
      '<td><input class="form-control form-control-alternative" type="text" name="price[]" placeholder="Price"></td>' +
      '<td><button type="button" name="remove" id="' + i + '" class="btn btn-sm btn-danger btn-remove">X</button></td></tr>');
  });

  // Remove inputs
  $(document).on('click', '.btn-remove', function() {
    var button_id = $(this).attr("id");
    $('#row' + button_id + '').remove();
  });

  $.ajaxSetup({
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-bordered" id="dynamic_field">
  <thead>
    <tr>
      <th scope="col">Description</th>
      <th scope="col">Qty</th>
      <th scope="col">Price</th>
      <th scope="col">Action</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><input class="form-control form-control-alternative" type="text" name="description[]" placeholder="Description"></td>
      <td><input class="form-control form-control-alternative" type="text" name="qty[]" placeholder="Qty"></td>
      <td><input class="form-control form-control-alternative" type="text" name="price[]" placeholder="Price"></td>
      <td><button type="button" name="add" id="add" class="btn btn-sm btn-success">+</button></td>
    </tr>
  </tbody>
</table>
...