2017-07-07 96 views
1

enter image description here在這裏我試圖從一個表(3列)追加到另一個。這很好。而且我需要一些額外的列。誰能幫幫我嗎。我的片段。添加額外的列,同時從表追加到另一個

function table2(){ 
$('#one').on("click", function(){ 
    $('#one tbody input:checked').parent().parent().appendTo("#two"); 
} 
+4

進一步解釋你的問題 –

+0

你想添加行到存在表和新創建的以前。這樣對嗎 ? – ToujouAya

+0

http://jsfiddle.net/fmuW6/11/ – Sinto

回答

0

這可能是有幫助的,這是一個例子。

// First of all this is an example, you has to modify this make as you wish 
// Here we can add dynamic columns & rows 
// Also , I have given an option to specify the row/column name 
<table border="1" id="mtable"> 
    <thead> 
     <tr> 
      <td>Item</td><td>Red</td><td>Green</td> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td>Some Item</td><td><input type="checkbox"/></td><td><input type="checkbox"/></td> 
     </tr> 
    </tbody> 
</table> 
<br/><br/> 
<input id="row" placeholder="Enter Item Name"/><button id="irow">Insert Row</button><br/><br/> 
<input id="col" placeholder="Enter Heading"/><button id="icol">Insert Column</button> 

<script> 
    // This is add additional row to a table 
    $('#irow').click(function(){ 
     if($('#row').val()){ 
      // Creates a copy of last row and you specify the name of row as well as 
      // each <td>. Here I'm given a checkbox. 
      $('#mtable tbody').append($("#mtable tbody tr:last").clone()); 
      $('#mtable tbody tr:last :checkbox').attr('checked',false); 
      $('#mtable tbody tr:last td:first').html($('#row').val()); 
     }else{alert('Enter Text');} 
    }); 
    $('#icol').click(function(){ 
     // This is add additional column to a table 
     if($('#col').val()){ 
      // This is to append/add column to a table 
      $('#mtable tr').append($("<td>")); 
      $('#mtable thead tr>td:last').html($('#col').val()); 
      // This is to add a column to each rows in a table 
      $('#mtable tbody tr').each(function(){ 
       $(this).children('td:last').append($('<input type="checkbox">')); 
      }); 
     } else { alert('Enter Text'); } 
    }); 
</script> 
+0

你不認爲這會是一個好主意來解釋爲什麼它會有用,或許可以解釋源代碼,以便其他人可以瞭解它的功能?如果你把它變成一個工作片段,並解釋源代碼,我會很樂意提供答案,但就目前而言,這看起來只是一個複製/粘貼,並希望他們可以使用它。有些人尋求幫助,但也希望瞭解解決方案/源代碼,而不僅僅是複製/粘貼,也可以幫助其他人在類似問題上遇到困難。 – NewToJS

+0

當然會解釋。 – Sinto

+0

你也可以檢查文檔https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement。 – Sinto