2017-09-22 43 views
0

我有一個HTML表格,每行都有一個複選框。 我想遍歷表格,如果有任何複選框被選中。 我需要創建一個只有選中複選框的行的新表。創建一個只有選中複選框的行的新表格

任何意見將不勝感激! 我知道如何穿過桌子。但如何將該行的HTML代碼存儲在變量中?

$('table tbody tr').each(function() { 
    var $tr = $(this); 
    if ($tr.find('input[type="checkbox"]').is(':checked')) { 

    } 
}); 

我想要做的是發送新表到新頁面。

function toView(){ 
    var newWindow = window.open();  
    var newHTML = '<html><head></head><body><table>'; 
    var endHtml = '</table></body></html>'; 


    $('table tbody tr').each(function() { 
     var $tr = $(this); 
     if ($tr.find('input[type="checkbox"]').is(':checked')) { 

      // what suppose to be here.... 

     } 
     }); 
    newHTML = new_table + endHtml 
    newWindow.document.write(newHTML); 
} 

回答

0

我找到了辦法!

function toView(){ 
    var newWindow = window.open();  
    var HTML_head = '<html><head></head><body><table>'; 
    var HTML_footer = '</table></body></html>'; 
    var new_HTML_page = ""; 
    var new_table = ""; 
    var html = ""; 

    $('table tbody tr input[type=checkbox]:checked').each(function() { 
     html = $(this).closest("tr").clone().find('tr:last').remove().end().prop('outerHTML'); 
     new_table += html;  
    });   
    newHTML = HTML_head + new_table + HTML_footer; 
    newWindow.document.write(newHTML); 
} 
0

你幾乎擁有它,如果你想創建一個表,其conents:

$(function() { 
    var newWindow = $('#window'); 
    $('table tbody tr input[type=checkbox]:checked').each(function() { 
     var $tr = $(this).parents('tr'); 
     var $new_table = $('<table>').append($tr); 
     newWindow.append($new_table); 
    }); 
}); 

Test it here

+0

對不起,但它只返回[對象對象]。這裏是我的全部功能: \t function toView(){ \t \t var newWindow = window.open(); \t \t \t 變種\t newHTML = '

'; \t \t var endHtml ='
'; \t \t變量$ NEW_TABLE = 「」 \t \t \t \t $( '表TBODY TR輸入[類型=複選框]:選中')每個(函數(){ \t \t變量$ tr_contents = $(本)。父母('tr'); \t \t $ new_table = $('').html($ tr_contents); \t \t}); \t \t \t newHTML = $ new_table + endHtml \t \t newWindow.document.write(newHTML); \t} –

+0

... newHTML = $ new_table.html()+ endHtml; newWindow.document.write(newHTML); 應該可以工作 – vnt

+0

它只接受最後選中的複選框。並且只返回單元格的值,沒有

和​​標籤。這會從當前頁面中刪除行。 –

相關問題