2013-05-31 42 views
0

我的最终目标是从索引中获取具有唯一标识的按钮。基于id被点击(即,我点击哪个按钮)它删除相应的表格行。例如,如果我点击一个ID为1的删除按钮,那么我想删除该表格中的第一个表格行。我无法理解我会如何做到这一点。使用唯一的按钮ID删除相应的表格行

这是我的代码。

count = ["<TR>", "<TR>", "<TR>", "<TR>"] 

$.each(count, function(index,value) { 
    index++; 
    $("#deletingRows table").append("<tr><td class='deleteRow' style='font-weight: bold;'>" + 'Row Number: ' + "<input type='text' name='rowNumber' id='rowNumber' style='margin-left: 45px;' value=" + index + ">" + "</input>" + "<input type='button' id='" + index + ' + "name='delete' value='Delete This Row'></input>" + "</td></tr>"); 
}); 

回答

1

您可以简化这: -

给类delete你的按钮。

$('.delete').click(function(){ 
    $(this).closest('tr').remove(); // closest('tr') will get you its parent tr and call .remove() to remove it. 
}); 

或者只是使用名称本身。

$('input[name=delete]').click(function(){ 
    $(this).closest('tr').remove(); // closest('tr') will get you its parent tr and call .remove() to remove it. 
    }); 

.closest()

记得把单击处理程序document.ready下。

+0

这太好了。看起来这会工作。 – wowzuzz

+0

@wowzuzz很棒..不客气。 – PSL

相关问题