2011-11-11 111 views
2

我有一个有4列的表,其中有一列有一个按钮。我希望当按钮收到点击按钮将被排除在外的表格行。JQuery表/按钮删除

由于表格行是通过单击另一个按钮动态插入的,因此按钮的ID是动态的,这使得我无法使用表格中按钮的ID作为选择器。

然后我会做下面的事情:在按钮上单击我想查找表中哪一行是按钮,然后删除这一行。

有什么建议吗?

我正在操作的表下方。

<table id="tbIngredients"> 
    <thead> 
      <tr> 
       <th id="cod"> Code </ th> 
       <th id="desc"> Description </ th> 
       <th id="price"> Price </ th> 
       <th id="op"> Operation </ th> 
      </ tr> 
    </ thead> 
    <tbody> 
      <tr> 
      <td id="tr1"> 1 </ td> 
      <td> Ingredient </ td> 
      <td> $ 1.00 </ td> 
      <td id="td1"> <input type="button" class="delIngredient" value="Excluir  ingrediente"> </ td> 
     </ tr> 
     <tr id="tr16"> 
      <td> 16 </ td> 
      <td> Papaya </ td> 
      <td> £ 01.05 </ td> 
      <td id="td16"> <input type="button" class="delIngredient" value="Excluir ingrediente"> </ td> 
     </ tr> 
    </ tbody> 
</ table>  

回答

2

怎么样,

$('.delIngredient').click(function(){ 
    $(this).parents('tr').remove(); 
}); 

错过了动态的,如果你同时页面加载后添加行,你需要将它添加到文档,

$(document).on('click', '.delIngredient', function(){ 
    $(this).parents('tr').remove(); 
}); 
0
$('.delIngredient').click(function() { 
    $(this).parent().parent().remove(); 
}); 

将删除父行。可能不是唯一的方法,但它适用于您的标记。

我想我更喜欢安德鲁斯解决方案。然后它不依赖于嵌套。

0

试试这个:

$("#tbIngredients").on("click", ".delIngredient", function() { 
    $(this).closest("tr").remove(); 
}); 

DEMO