简体   繁体   English

实施删除动态创建的表行的解决方案所需的指导

[英]Guidance Needed to Implement Solution to Deleting Dynamically Created Table Rows

I have a simple table that by default shows a single row, with an add button for users to add additional rows if required.我有一个简单的表格,默认情况下显示单行,如果需要,用户可以使用添加按钮添加其他行。 I have a delete icon showing in the last column for users to delete a row, however this only works for the first table row and doesn't work for any rows the user has added themselves.我在最后一列中显示了一个删除图标,供用户删除一行,但这仅适用于第一个表格行,不适用于用户自己添加的任何行。

I understand the issue is in this answer and relates to the objects that exist when the page is first loaded, but I can't work out how to implement that solution in my example as I'm only just learning Javascript and jQuery here.我知道问题出在这个答案中,并且与首次加载页面时存在的对象有关,但我无法在我的示例中解决如何实现该解决方案,因为我只是在这里学习 Javascript 和 jQuery 。

Here's a sample of how this looks:这是外观的示例:

 $(document).ready(function() { $("#addRow").click(function() { var markup = "<tr><td><input type=\"text\" class=\"form-control\" autocomplete=\"off\" placeholder=\"Serial\" name=\"serial\"></td><td></td><td></td><td></td><td><span class=\"glyphicon glyphicon-trash\"></span></td></tr>"; $("#shipmentConsignment").append(markup); }); // Find and remove selected table rows $("#deleteRow").click(function() { $(this).closest('tr').remove(); }); });
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <table id="shipmentConsignment" class="table table-condensed table-striped table-bordered"> <thead> <th class="text-center" scope="col" width="20%">Serial</th> <th class="text-center" scope="col" width="15%">ID</th> <th class="text-center" scope="col" width="15%">Code</th> <th class="text-center" scope="col" width="45%">Description</th> <th class="text-center" scope="col" width="5%"></th> </thead> <tbody> <tr> <td><input type="text" class="form-control" autocomplete="off" placeholder="Serial" name="serial"></td> <td></td> <td></td> <td></td> <td id="deleteRow"><span class="glyphicon glyphicon-trash"></span></td> </tr> </tbody> </table> <button type="button" name="addRow" value="addRow" id="addRow" class="btn btn-primary">Add Item</button>

You can use .on("click", "selector", fuction() {}) to perform what you need, the example below works as requested.您可以使用.on("click", "selector", fuction() {})来执行您需要的操作,下面的示例按要求工作。 You can't bind an event trigger to an element that doesn't exist yet, so you need to bind it to something that does (ie the table) and then listen for click events on the selector .deleteRow .您无法将事件触发器绑定到尚不存在的元素,因此您需要将其绑定到可以执行的操作(即表格),然后在选择器.deleteRow上侦听click事件。

Your event isnt triggering because there's no id assigned to your dynamically created row cell for the delete button.您的事件没有触发,因为没有为删除按钮动态创建的行单元格分配id BUT, you shouldn't re-use an id for multiple elements so I would switch to using a class as I have below.但是,您不应该为多个元素重复使用id ,所以我将切换到使用class ,如下所示。


Demo演示

 $(document).ready(function() { $("#addRow").click(function() { var markup = "<tr><td><input type=\"text\" class=\"form-control\" autocomplete=\"off\" placeholder=\"Serial\" name=\"serial\"></td><td></td><td></td><td></td><td class='deleteRow'><span class=\"glyphicon glyphicon-trash\"></span></td></tr>"; $("#shipmentConsignment").append(markup); }); // Find and remove selected table rows $("#shipmentConsignment").on("click", ".deleteRow", function() { $(this).closest('tr').remove(); }); });
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <table id="shipmentConsignment" class="table table-condensed table-striped table-bordered"> <thead> <th class="text-center" scope="col" width="20%">Serial</th> <th class="text-center" scope="col" width="15%">ID</th> <th class="text-center" scope="col" width="15%">Code</th> <th class="text-center" scope="col" width="45%">Description</th> <th class="text-center" scope="col" width="5%"></th> </thead> <tbody> <tr> <td><input type="text" class="form-control" autocomplete="off" placeholder="Serial" name="serial"></td> <td></td> <td></td> <td></td> <td class="deleteRow"><span class="glyphicon glyphicon-trash"></span></td> </tr> </tbody> </table> <button type="button" name="addRow" value="addRow" id="addRow" class="btn btn-primary">Add Item</button>

Keep in mind that the id attribute is meant to be unique.请记住,id 属性是唯一的。

what you can do is delegate the deletion to the table您可以做的是将删除委托给表

$(document).ready(function() {
  $("#addRow").click(function() {
    var markup = "<tr><td><input type=\"text\" class=\"form-control\" autocomplete=\"off\" placeholder=\"Serial\" name=\"serial\"></td><td></td><td></td><td></td><td><span class=\"glyphicon glyphicon-trash\"></span></td></tr>";
    $("#shipmentConsignment").append(markup);
  });

  // only delete row if the glyphicon-trash was clicked
  $('#shipmentConsignment').click(function (ev) {
    if (ev.target.matches('.glyphicon-trash')) {
      $(ev.target).closest('tr').remove();
    }
  });
});

As you've clearly seen the duplicate and are still stuck, here's what to do and how it works正如您已经清楚地看到了重复并且仍然卡住了,这就是要做什么以及它是如何工作的

What to do.该怎么办。 Change your structure to use a class instead of an ID because you can't use the same ID over and over.更改您的结构以使用 class 而不是 ID,因为您不能一遍又一遍地使用相同的 ID。 Each must be unique.每个都必须是唯一的。 Note the <td class="deleteRow"> below注意下面的<td class="deleteRow">

<tr>
      <td><input type="text" class="form-control" autocomplete="off" placeholder="Serial" name="serial"></td>
      <td></td>
      <td></td>
      <td></td>
      <td class="deleteRow"><span class="glyphicon glyphicon-trash"></span></td>
    </tr>

Then change your ADD function to reflect this然后更改您的 ADD function 以反映这一点

  $("#addRow").on('click',function() {
    var markup = "<tr><td><input type=\"text\" class=\"form-control\" autocomplete=\"off\" placeholder=\"Serial\" name=\"serial\"></td><td></td><td></td><td></td><td class='deleteRow'><span class=\"glyphicon glyphicon-trash\"></span></td></tr>";
    $("#shipmentConsignment").append(markup);
  });

Finally, update your listener to be a delegate.最后,将您的侦听器更新为委托。 This means you attach the listener to a static containing element (one that is not dynamically created) and reference the actual element you want in the delegate.这意味着您将侦听器附加到包含元素(非动态创建的元素)的 static 并在委托中引用您想要的实际元素。 That way any newly added rows will keep their functionality这样,任何新添加的行都将保留其功能

  // Find and remove selected table rows
  $("#shipmentConsignment").on('click', '.deleteRow', function() {
    $(this).closest('tr').remove();
  });

More about jQuerys implementation of event delegation: https://api.jquery.com/on/更多关于事件委托的jQuerys实现: https://api.jquery.com/on/

This is because the initial row is different from the ones being appended.这是因为初始行与附加的行不同。 To solve this, use class for deleteRow instead of id to group all columns to be used in the click handler as shown below:要解决此问题,请使用deleteRow代替 id 来对单击处理程序中要使用的所有列进行分组,如下所示:

 $(document).ready(function() { // define markup and append one row const markup = "<tr><td><input type=\"text\" class=\"form-control\" autocomplete=\"off\" placeholder=\"Serial\" name=\"serial\"></td><td></td><td></td><td></td><td class=\"deleteRow\"><span class=\"glyphicon glyphicon-trash\"></span></td></tr>"; $("#shipmentConsignment").append(markup); // append markup on #addRow click $("#addRow").click(function() { $("#shipmentConsignment").append(markup); }); // remove closest row to the clicked element with class deleteRow $(document).on("click", ".deleteRow", function() { $(this).closest('tr').remove(); }); });
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <table id="shipmentConsignment" class="table table-condensed table-striped table-bordered"> <thead> <th class="text-center" scope="col" width="20%">Serial</th> <th class="text-center" scope="col" width="15%">ID</th> <th class="text-center" scope="col" width="15%">Code</th> <th class="text-center" scope="col" width="45%">Description</th> <th class="text-center" scope="col" width="5%"></th> </thead> <tbody> </tbody> </table> <button type="button" name="addRow" value="addRow" id="addRow" class="btn btn-primary">Add Item</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM