2017-06-05 133 views
0

实际上,我正在寻找将一个div(表1)的表中复选的行复制到另一个div(表2)的表中。但对于下面的脚本,当我检查多个复选框时,第一个选中的行将被最后一个选中的行替换。但是我希望表1的每个检查行都应该逐一列在表2中。复制从一个表到另一个表的复选行

任何帮助表示赞赏。提前致谢!!!

HTML

<div class="form-group" id="jqcc" > 

     <table id="order-table" name="order-table"> 
      <tr> 
       <th>Select</th> 
       <th>Medication</th> 
       <th>Max Issue Limit</th>               
      </tr> 
      <tbody></tbody>             
     </table>             
</div> 

的Javascript

<script> 
     var tableControl = document.getElementById('order-table'); 
     $('#jqcc').click(function() { 
     var result = [] 
     var x1; 
     var tab; 

     var count = document.querySelectorAll('input[type="checkbox"]:checked').length; 


     $('input:checkbox:checked', tableControl).each(function() {       

      result.push($(this).parent().next().text() + "," + "<br>"); // refers title of product 

      $(this).closest('tr').find('input').each(function() { 

      x1 = $(this).val(); // refers price of product 


     }) 

     tab = "<tr><td>" + result + "</td>"; 
     tab += "<td>" + x1 + "</td>"; 
     tab += "<td>" + x1 + " </td>"; 
     tab += "<td><button type='button' onclick='deletePerson(this);' class='btn btn-default'> <span class='glyphicon glyphicon-remove' /></button></td>"; 
     tab += "</tr>"; 
     tab += "</tbody>" 

     $("#charges").html("<table id='peopleTable' class='table table-bordered table-condensed table-striped'><thead><tr><th>Description</th><th>Actual Amount</th> <th>Claim Amount</th> <th></th></tr></thead>" + tab + "</table>").show(); 

    }); 

}); 
</script> 
+1

您可以添加您的HTML请 –

回答

1

你尝试改变:

<script> 
    var tableControl = document.getElementById('order-table'); 
    $('#jqcc').click(function() { 
    var result = [] 
    var x1; 
    var tab = ""; 

    var count = document.querySelectorAll('input[type="checkbox"]:checked').length; 
    $("#charges").html("<table id='peopleTable' class='table table-bordered table-condensed table-striped'><thead><tr><th>Description</th><th>Actual Amount</th><th>Claim Amount</th> <th></th></tr></thead><tbody></tbody></table>").show(); 

    $('input:checkbox:checked', tableControl).each(function() {       

     result.push($(this).parent().next().text() + "," + "<br>"); // refers title of product 

     $(this).closest('tr').find('input').each(function() { 

     x1 = $(this).val(); // refers price of product 

     // 
     tab = "<tr><td>" + result + "</td>"; 
     tab += "<td>" + x1 + "</td>"; 
     tab += "<td>" + x1 + " </td>"; 
     tab += "<td><button type='button' onclick='deletePerson(this);' class='btn btn-default'> <span class='glyphicon glyphicon-remove' /></button></td>"; 
     tab += "</tr>"; 


     // append row html to table with id 
     $("#peopleTable").append(tab); 

    })  
}); 

});

+0

这个解决方案的工作,但仍然是产品的标题有一些问题。当我选择第二个复选框时,第一个选中的复选框标题也会聚集在一起@le hung – Muzz

+1

案例1:如果table1与table2具有相同的列,则可以将html行table1追加到table2, 案例2:您需要重置所有变量默认在foreach –

+0

非常感谢@le挂起 – Muzz

相关问题