简体   繁体   English

克隆表中的一行而不会破坏该行中的按钮

[英]Cloning a row in a table without breaking a button in the row

I'm a beginner currently trying to learn how to code, and my current project is to renovate a website. 我是一个初学者,目前正在尝试学习编码,而我目前的项目是翻新网站。 The website needs a table with an inbuilt save/edit function, which I have already implemented, but it also needs an "add new row" function which I can't seem to get to work. 该网站需要一个带有内置保存/编辑功能的表,我已经实现了该功能,但是它还需要一个“添加新行”功能,我似乎无法使用它。 Does anyone know how I might be able to clone a row in my table, whilst keeping the button functional? 有谁知道在保持按钮正常工作的情况下,如何能够在表中克隆行? a previous attempt managed to clone the row, but the button broke. 先前的尝试成功克隆了该行,但是按钮坏了。 Does anyone have a solution to this problem? 有谁能解决这个问题?

Here is the code i'm currently working with in fiddle: 这是我目前正在处理的代码:

HTML: HTML:

  <table id="tableone" border="2" cellspacing="2" cellpadding="2">
  <thead>
  <tr>
  <th class="col1">Seminar Name</th>
  <th class="col2">Seminar Date</th>
  <th class="col3">Download Link</th>
  <tbody>
  <tr class="del">
  <td contenteditable="false"></td>
  <td contenteditable="false"></td>
  <td contenteditable="false"></td>
  <td>
  <button class="editbtn">Edit</button>
  </td>
  </tr>
  <tr class="del" id="tablerow">
  <td contenteditable="false"></td>
  <td contenteditable="false"></td>
  <td contenteditable="false"></td>
  <td>
  <button class="editbtn">Edit</button>
  </td>
  </tr>
  </tbody>
  <button class="addnewrow">Add New Row</button>

JAVASCRIPT: Frameworks/extensions: jQuery 1.11.0 JAVASCRIPT:框架/扩展:jQuery 1.11.0

$('.editbtn').click(function() {
var $this = $(this);
var tds = $this.closest('tr').find('td').filter(function() {
return $(this).find('.editbtn').length === 0;
});
if ($this.html() === 'Edit') {
$this.html('Save');
tds.prop('contenteditable', true);
} else {
$this.html('Edit');
tds.prop('contenteditable', false);
}
});
$('.addnewrow').click(function() {
var $tr = $(this).closest('tr');
var $clone = $tr.clone();
$clone.find('input').val('');
$tr.after($clone);
});

Note: for some reason, the button to clone the previous row just stopped working altogether, and the code for the new row seems to have broken the "save" and "edit" code. 注意:由于某种原因,克隆上一行的按钮完全停止了工作,新行的代码似乎破坏了“保存”和“编辑”代码。 Additionally, would it be possible to hide the "save" and "edit" buttons so that only people with admin perms can see/interact with them? 另外,是否可以隐藏“保存”和“编辑”按钮,以便只有具有管理员权限的人才能看到/与他们互动?

Here is the fixed code add new row will copy your last row and append it to the table.To solve your problem just bind the click event using document. 这是固定的代码,添加新行将复制您的最后一行并将其附加到表中。要解决您的问题,只需使用文档绑定click事件即可。 Even on click might not work sometimes but this will always do. 即使单击,有时也可能无法正常工作,但这总是可以的。

 $(document).on("click",".editbtn",function() { console.log("Edit called"); var $this = $(this); var tds = $this.closest('tr').find('td').filter(function() { return $(this).find('.editbtn').length === 0; }); if ($this.html() === 'Edit') { $this.html('Save'); tds.prop('contenteditable', true); } else { $this.html('Edit'); tds.prop('contenteditable', false); } }); $('.addnewrow').click(function() { var $tr = $("#tableone").find("tr:last"); var $clone = $tr.clone(); $clone.find('input').val(''); $tr.after($clone); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="tableone" border="2" cellspacing="2" cellpadding="2"> <thead> <tr> <th class="col1">Seminar Name</th> <th class="col2">Seminar Date</th> <th class="col3">Download Link</th> <tbody> <tr class="del"> <td contenteditable="false"></td> <td contenteditable="false"></td> <td contenteditable="false"></td> <td> <button class="editbtn">Edit</button> </td> </tr> <tr class="del" id="tablerow"> <td contenteditable="false"></td> <td contenteditable="false"></td> <td contenteditable="false"></td> <td> <button class="editbtn">Edit</button> </td> </tr> </tbody> <button class="addnewrow">Add New Row</button> 

Maybe you want to clone the button and it's click event and callback,so you should use the event proxy,your code doesn't work: 也许您想克隆按钮,它是单击事件和回调,所以您应该使用事件代理,您的代码不起作用:

$('.editbtn').click(function() {});

instead modify to: 改为修改为:

$('#tableone').on('click', '.editbtn', function() {});

and last modify the .addnewrow : 最后修改.addnewrow

var $tr = $("#tableone").find("tr:last");

 $('#tableone').on('click', '.editbtn', function() { var $this = $(this); var tds = $this.closest('tr').find('td').filter(function() { return $(this).find('.editbtn').length === 0; }); if ($this.html() === 'Edit') { $this.html('Save'); tds.prop('contenteditable', true); } else { $this.html('Edit'); tds.prop('contenteditable', false); } }); $('.addnewrow').click(function() { var $tr = $("#tableone").find("tr:last"); var $clone = $tr.clone(); $clone.find('input').val(''); $tr.after($clone); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="tableone" border="2" cellspacing="2" cellpadding="2"> <thead> <tr> <th class="col1">Seminar Name</th> <th class="col2">Seminar Date</th> <th class="col3">Download Link</th> </thead> <tbody> <tr class="del"> <td contenteditable="false"></td> <td contenteditable="false"></td> <td contenteditable="false"></td> <td> <button class="editbtn">Edit</button> </td> </tr> <tr class="del" id="tablerow"> <td contenteditable="false"></td> <td contenteditable="false"></td> <td contenteditable="false"></td> <td> <button class="editbtn">Edit</button> </td> </tr> </tbody> <button class="addnewrow">Add New Row</button> 

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

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