简体   繁体   English

将表行复制到另一个表HTML

[英]Copy Table row to another table HTML

Fiddle 小提琴

I have two tables. 我有两张桌子。 When I click on one table row it will copy to another table, and I will change the button as my requirement. 当我点击一个表格行时,它将复制到另一个表格,我将根据我的要求更改按钮。 Please help me. 请帮我。

$(window).load(function() {
                var items = [];

                $(".addBtn").on("click", function() {
                    var newTr = $(this).closest("tr").clone();

                    var newButtonHTML = "<input type = 'button' value='Edit' onclick='Edit()' /><input type='button' value='Delete' onclick='deleteRow(this.parentNode.parentNode.rowIndex)'/>";
                    $(newButtonHTML).children("button").click(function(e) {
                    });

                    $(newTr).children("td:last").html("").html(newButtonHTML);
                    items.push(newTr);
                    newTr.appendTo($("#stopsTable"));

                });

At your example it was a wrong selector 在你的例子中,这是一个错误的选择器

$('#myTable.tr') 

instead of 代替

$('#myTable tr')

Check fixed version: http://jsfiddle.net/V7wXD/7/ 检查固定版本: http//jsfiddle.net/V7wXD/7/

Best regards! 最好的祝福!

Js: JS:

$(function() {
    $('#myTable tbody tr').each(function(){
        $(this).append('<td><button class="copy">Copy</button></td>');
    });

    $(document).on('click', 'button.copy', function(){
        var $tr = $(this).parents('tr:first').clone();

        $tr.appendTo($('#stopsTable > tbody'));
    });
});

Test here: 在这里测试:

http://jsfiddle.net/V7wXD/8/ http://jsfiddle.net/V7wXD/8/

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

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