简体   繁体   English

单击相应的删除按钮从表中删除行

[英]Delete row from table on corresponding delete button click

I'm facing some problem in the given example. 在给定的示例中,我遇到了一些问题。 I have a list of three items, all of them have a delete button, when I click on delete button the corresponding row is deleted. 我有三个项目的列表,所有项目都有一个删除按钮,当我单击删除按钮时,相应的行将被删除。 It's works fine. 很好

Now my problem is if I have all three item in the list and I want to delete only first item and I click on first delete button it removes all the rows of the table. 现在我的问题是,如果我在列表中同时拥有所有三个项目,并且只想删除第一个项目,然后单击“第一个删除”按钮,它将删除表中的所有行。

Demo Here Here演示

That's because your first id is 0 so its deleting all elements, remove the if condition and else block it works fine, if you want to delete entire table then add other button. 那是因为您的第一个id为0,所以它删除了所有元素,删除了if条件,否则阻止了它的正常运行,如果要删除整个表,则添加其他按钮。

$('input[type=button]').click(function () {
    $(this).closest("tr").remove();
});

Working Fiddle 工作小提琴

There is one basic flaw in your Code. 您的代码中有一个基本缺陷。

You were binding on click to just button tag, but in your HTML there were no button tags, there were input tags with button type. 您只能将click绑定到按钮标签,但是在HTML中没有按钮标签,有带有按钮类型的输入标签。

After this you are creating a variable called "id" which is unnecessary in this case. 此后,您将创建一个名为“ id”的变量,在这种情况下,此变量是不必要的。 With jQquery you do not need this. 使用jQquery,您不需要此。

Using the following line of code takes care of everything. 使用以下代码行可以处理所有事情。 So simplify life and make this the jQuery as short as possible. 因此,简化生活,并使其尽可能短。

$(this).parent().parent().remove();

As far as you #divGrid being empty is a false statement as the div will never be empty unless you remove the entire #myTable child element 就#divGrid为空而言,这是一个错误的语句,因为div永远不会为空,除非您删除整个#myTable子元素

You can do that by using the following condition 您可以使用以下条件来做到这一点

if( $('#divGrid ').is(':empty') ) {
   $('#divGrid').html("All item removed");
}

Here is the solution with just 3 lines of jQuery that you really need. 这是您真正需要的仅3行jQuery的解决方案

EDIT: Here is a fiddle to check the rows being empty and if empty then the table will be replaced by the text "All items removed" 编辑:这是一个小提琴,以检查行为空,如果为空,则表将被文本“所有项目已删除”替换

Working Fiddle 工作小提琴

try this 尝试这个

$(':button').click(function() {  
    $(this).closest("tr").remove();
});

Demo http://jsfiddle.net/BLV2g/14/ 演示http://jsfiddle.net/BLV2g/14/

to be more safer the button will not delete else where 为了更安全,按钮将不会删除其他地方

$('#myTable :button').click(function() {  
    $(this).closest("tr").remove();
});

Try this, 尝试这个,

function delSpec(rl)
  {    
     r=document.getElementById("insSpecc").rows.length;
     if(r!=2){
       document.getElementById("insSpecc").deleteRow(r-1)
     }
  }

Demo http://jsfiddle.net/nKkhW/111/ 演示http://jsfiddle.net/nKkhW/111/

I guess this is what you want to do: 我想这就是您想要做的:

$('#myTable :button').click(function () {
  $(this).parent().parent().remove();
  var rowCount = $('#myTable tr').length;
  if (rowCount == 1) {
    $('#divGrid').empty();
    $('#divGrid').html("All item removed");
  }
});

The logic behind is: 背后的逻辑是:

  1. whenever an input type button is clicked, trigger the jquery event. 只要单击输入类型按钮,就会触发jquery事件。
  2. remove the tr content where the button is in. 删除按钮所在的tr内容。
  3. check if there are rows except the title row, if only title row, replace the title. 检查标题行以外是否还有其他行;如果只有标题行,则替换标题。

Demo 演示版

由于第一个删除按钮的ID为del0 ,它将进入else子句并删除所有三行。

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

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