繁体   English   中英

克隆最近的表格行

[英]Cloning closest table row

我最近问了这个问题

从那时起,我发现自己想做的事行不通。 这是因为该表是由for循环生成的,其id每次都会递增。 因此,我需要修改一些东西,以便它也可以为该表添加行。 我已经更新了小提琴以显示带有两个表JSFiddle的示例

基本上,我现在要这样做

$(function() {
    $(".addCF").click(function(){
       var clone = $(this).closest('tr').clone(true).insertAfter($(this).closest('tr'));
    });
    $("#customFields").on('click','.remCF',function(){
        $(this).parent().parent().remove();
    });
});

我可以使用前面的示例自己完成克隆行的名称和标签。 我的主要问题是我不想克隆整个行。 目前,它正在克隆

<tr>
    <td><label class="subjectline" for="User1">User NOC M1</label></td>
    <td id="slLabel">SL_A</td>
    <td id="slInput"><input type="text" name="slOptions[User][NOC M1]" class="form-control" id="User1"></td>
    <td><a class="addCF" href="javascript:void(0);">+ additional user</a></td>
</tr> 

我需要它来克隆它,但将第一个td设为空。 另外,像最初的问题一样,最后一个td应该是一个关闭按钮

<a href="javascript:void(0);" class="remCF">Remove</a>

是否有可能做到这一点?

谢谢

从克隆的tr

  1. emptyfirst-childtd )的内容。
  2. 将其last-child (td)的html设置为close元素的html,

$(function() {
    $(".addCF").click(function(){
       var clone = $(this).closest('tr').clone(true);
       $("td:first-child", clone).empty();
       $("td:last-child", clone).html('<a href="javascript:void(0);" class="remCF">Remove</a>');
       clone.insertAfter( $(this).closest('tr'));
    });
    $("table.table").on('click','.remCF',function(){
        $(this).parent().parent().remove();
    });
});

DEMO

您需要更改克隆的TR的html:

HTML:

<table id="customFields1" class="table table-bordered table-hover additionalMargin alignment">
    <thead>
    <tr>
        <th colspan="2"></th>
        <th>Some Title</th>
    </tr>
    </thead>
    <tbody>
        <tr>
            <td><label class="subjectline" for="User1">User NOC M1</label></td>
            <td id="slLabel">SL_A</td>
            <td id="slInput"><input type="text" name="slOptions[User][NOC M1]" class="form-control" id="User1"></td>
            <td><a class="addCF" href="javascript:void(0);">+ additional user</a></td>
        </tr> 

    </tbody>
</table>

<table id="customFields2" class="table table-bordered table-hover additionalMargin alignment">
    <thead>
    <tr>
        <th colspan="2"></th>
        <th>Some Title</th>
    </tr>
    </thead>
    <tbody>
        <tr>
            <td><label class="subjectline" for="User1">User NOC M1</label></td>
            <td id="slLabel">SL_A</td>
            <td id="slInput"><input type="text" name="slOptions[User][NOC M1]" class="form-control" id="User1"></td>
            <td><a class="addCF" href="javascript:void(0);">+ additional user</a></td>
        </tr> 
    </tbody>
</table>

JQUERY:

$(function() {
    $(".addCF").click(function(){
       var closest_tr =$(this).closest('tr').clone(true); 
       closest_tr = $(closest_tr).find("td:first").html("").parent();
       var clone = closest_tr.insertAfter( $(this).closest('tr'));
    });
    $("#customFields").on('click','.remCF',function(){
        $(this).parent().parent().remove();
    });
});

演示: https : //jsfiddle.net/116hvkhe/

这是您正在寻找的东西吗: https : //jsfiddle.net/8zwf9njr/13/

$(".addCF").click(function(){
    var clone = $(this).closest('tr').clone(true);
    clone.find('td:first-child').html('');
    clone.find('td:last-child').html('<a href="javascript:void(0);" class="remCF">Remove</a>');
    clone.insertAfter( $(this).closest('tr'));
});

您会找到克隆的第一个td元素并将其清空。 然后找到克隆的最后一个td ,然后用删除按钮替换其中的所有内容。

PS。 我认为您原来的删除点击代码中也有一个错字:应该是$("#customFields1").on('click',而不是#customFields 。或者,您也可以使用更通用的选择器(例如table来定位两个table或在表中添加一个类并使用它。

暂无
暂无

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

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