最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Delete row from table on corresponding delete button click - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question edited Aug 25, 2015 at 18:12 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 4, 2013 at 6:21 S. S. RawatS. S. Rawat 6,1314 gold badges44 silver badges59 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 4

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.

$('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.

After this you are creating a variable called "id" which is unnecessary in this case. With jQquery you do not need this.

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

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.

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/BLV2g/14/

to be more safer the button will not delete else where

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

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.
  2. remove the tr content where the button is in.
  3. check if there are rows except the title row, if only title row, replace the title.

Demo

Try this,

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

Demo http://jsfiddle/nKkhW/111/

Since first delete button id is del0 it will go to the else clause and delete all the three rows.

发布评论

评论列表(0)

  1. 暂无评论