使用jQuery将表行从一个表复制到另一个表

时间:2022-11-03 09:49:09

I need to be able to copy a row from one table to the other using jQuery when a checkbox in that row is checked.

我需要能够在检查该行中的复选框时使用jQuery将一行从一个表复制到另一个表。

I tried using the .clone() method but this had very strange behaviour. Even though the jQuery was specifically targeting rows in the second table, when copied to the first, it could still be targeted. So either clone() is not right or I need to use something else.

我尝试使用.clone()方法,但这有非常奇怪的行为。尽管jQuery专门针对第二个表中的行,但是当复制到第一个表时,它仍然可以成为目标。所以要么clone()不正确,要么我需要使用别的东西。

$('#offers tbody tr td input.checkbox:not(:checked)').click(function (e) {
    var row = $(this).closest('tr');
});

1 个解决方案

#1


6  

$('#offers tbody tr td input.checkbox:not(:checked)').on('change', function (e) {
     var row = $(this).closest('tr').html();
     $('#otherTable tbody').append('<tr>'+row+'</tr>');
});

See it in action! http://jsfiddle.net/3BZp4/1/

看到它在行动! http://jsfiddle.net/3BZp4/1/

Clone also SHOULD work for you and not effect future selection:

克隆也应该为你工作,而不会影响未来的选择:

http://jsfiddle.net/AkVTw/1/

Now with catch for unchecking as per comments:

现在根据评论抓取取消选中:

http://jsfiddle.net/wGGDb/

#1


6  

$('#offers tbody tr td input.checkbox:not(:checked)').on('change', function (e) {
     var row = $(this).closest('tr').html();
     $('#otherTable tbody').append('<tr>'+row+'</tr>');
});

See it in action! http://jsfiddle.net/3BZp4/1/

看到它在行动! http://jsfiddle.net/3BZp4/1/

Clone also SHOULD work for you and not effect future selection:

克隆也应该为你工作,而不会影响未来的选择:

http://jsfiddle.net/AkVTw/1/

Now with catch for unchecking as per comments:

现在根据评论抓取取消选中:

http://jsfiddle.net/wGGDb/