【问题标题】:Delete corresponding table row with unique button id删除具有唯一按钮 id 的对应表行
【发布时间】:2013-05-31 20:48:13
【问题描述】:

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

这是我的代码。

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>");
});

【问题讨论】:

    标签: javascript jquery


    【解决方案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下面。

    【讨论】:

      猜你喜欢
      • 2016-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多