Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jQuery将数据从一个表的选定行复制到另一个表_Javascript_Jquery_Datatables - Fatal编程技术网

Javascript 使用jQuery将数据从一个表的选定行复制到另一个表

Javascript 使用jQuery将数据从一个表的选定行复制到另一个表,javascript,jquery,datatables,Javascript,Jquery,Datatables,我有两张表,其中一张有我的产品数据,如名称和条形码。 另一个是空的,我想通过jQuery将products的表(仅选定行)复制到第二个表中 <table id="exampleTable1" style="max-width:50%;"> <thead> <tr> <th>bar-code</th> <th>product name</th>

我有两张表,其中一张有我的产品数据,如名称和条形码。 另一个是空的,我想通过jQuery将products的表(仅选定行)复制到第二个表中

<table id="exampleTable1" style="max-width:50%;">
    <thead>
        <tr>
            <th>bar-code</th>
            <th>product name</th>
        </tr>
    </thead>
    <tbody>
        <tr role="row" class="odd selected">
            <td class="sorting_1">545333456</td>
            <td>Galaxy S9</td>
        </tr>
        <tr role="row" class="even selected">
            <td class="sorting_1">876543</td>
            <td>Galaxy S6</td>
        </tr>
        <tr role="row" class="odd">
            <td class="sorting_1">407654</td>
            <td>SD 64G </td>
        </tr>
        <tr role="row" class="even selected">
            <td class="sorting_1">876543</td>
            <td>Galaxy S5</td>
        <tr role="row" class="odd">
            <td class="sorting_1">407654</td>
            <td>Iphone 7 </td>
        </tr>
    </tbody>
</table>

条形码
产品名称
545333456
星系S9
876543
银河S6
407654
SD 64G
876543
银河S5
407654
iPhone7
我的第二张桌子:

<table id="exampleTable2" style="max-width:50%;">
    <thead>
        <tr>
            <th>bar-code</th>
            <th>product name</th>
        </tr>
    </thead>
    <tbody>
        <tr>
        </tr>
        <tr>
        </tr>
    </tbody>
</table>

<button class="btn btn-success" data-panelId="copy1" id="copy1">
    Copy from exampleTable1 To exampleTable1
</button>

条形码
产品名称
从exampleTable1复制到exampleTable1

有几个jQuery方法可以使这项工作变得简单,即and方法。您可以通过以下方式实现您想要的:

$('#copy1').click(function() {

$('tr.selected', '#exampleTable1').each(function() {

    // For each "selected" row of table1 ..
    var rowFromTable1 = $(this);

    // .. Take a clone/copy of it ..
    var clonedRowFromTable1 = rowFromTable1.clone();

    // .. And append the cloned row to the tbody of table2
    $('tbody', '#exampleTable2').append( clonedRowFromTable1 )
 })

 })

作品非常感谢。