简体   繁体   English

从一个表的行中导入值到另一个表的 header

[英]Import values from rows of one table to header of another table

I want to import values from a row of one table to the header of another table.我想将一个表的一行中的值导入到另一个表的 header 中。 The issue is that my code copies the selected row to a div where table 2 is present.问题是我的代码将选定的行复制到存在表 2 的 div 中。

Also, the row has an input field in which I can enter a value.此外,该行有一个input字段,我可以在其中输入一个值。 It copies the input field as a whole and not just the value.它复制整个输入字段,而不仅仅是值。 How can I import only the value of the input field?如何仅导入输入字段的值?

 var table; $('#createPlist').click(function plrList() { count = Number($('#noofTiks').val()); caption = $('<caption>'); table = $('<table>'); table = $(table).css({ "border": "1px solid black" }); var thead = $('<thead>'); var htr = $('<tr>'); var tbody = $('<tbody>'); for (var a = 0; a < count; a++) { var row = $('<tr>'); for (var b = 0; b < 2; b++) { var td = $("<td>"); td = $(td).css({ "border": "1px solid black" }); if (b == 0) { var srNo = a + 1; td.append(srNo); } else { var input = $('<input>'); td.append(input); } row.append(td); } tbody.append(row); } var th = $('<th>'); th.append("Sr. No."); htr.append(th); th = $('<th>'); th.append("Player Name"); htr.append(th); thead.append(htr); table.append(thead); table.append(tbody); caption.append("Player List"); $('#pList').append(caption); $('#pList').append(table); }); $('#createPlist').on('click', function() { $(this).prop('disabled', true); }); $('#giveTik').click(function() { count = Number($('#noofTiks').val()); for (h = 0; h < count; h++) { var table = $('<table>'); table = $(table).css({ "border": "1px solid black", "margin": "20px auto", "width": "500px", "height": "250px" }) var thead = $('<thead>'); var tbody = $('<tbody>'); for (var i = 0; i < 3; i++) { // append new row to body var row = $('<tr>'); //var row = ''; for (var j = 0; j < 9; j++) { // append new td to row var td = $("<td>"); td = $(td).css({ "border": "1px solid black", }) td.append(""); row.append(td); } tbody.append(row); } var showTable = $('#pList').find('tr').eq(h + 1); //NEED HELP HERE console.log(showTable) // For "selected" row of table1.. var rowFromTable1 = showTable; //.. Take a clone/copy of it.. var clonedRowFromTable1 = rowFromTable1.clone(); //NEED HELP HERE //.. And append the cloned row to the thead of table2 $('#div').append(clonedRowFromTable1); // thead.append(h + 1); // table.append(thead); table.append(tbody); $('#div').append(table); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" id="noofTiks"> <div id="pList"></div> <button class="btn btn-error" id="createPlist">OK</button> <div id="div"></div> <button id="giveTik">CREATE TICKETS</button>

Your current code was appending row to div because you where directly appending the clone row to #div instead you should append the cloned row to thead and then append this thead to table .Also i have replace cloned row td with th and added colspan .您当前的代码将行附加到 div 因为您直接将克隆行附加到#div而不是您应该 append 克隆行到thead然后 append 这个theadtable 。另外我已经用th替换了克隆行td并添加了colspan

Demo Code :演示代码

 var table; $('#createPlist').click(function plrList() { count = Number($('#noofTiks').val()); caption = $('<caption>'); table = $('<table>'); table = $(table).css({ "border": "1px solid black" }); var thead = $('<thead>'); var htr = $('<tr>'); var tbody = $('<tbody>'); for (var a = 0; a < count; a++) { var row = $('<tr>'); for (var b = 0; b < 2; b++) { var td = $("<td>"); td = $(td).css({ "border": "1px solid black" }); if (b == 0) { var srNo = a + 1; td.append(srNo); } else { var input = $('<input>'); td.append(input); } row.append(td); } tbody.append(row); } var th = $('<th>'); th.append("Sr. No."); htr.append(th); th = $('<th>'); th.append("Player Name"); htr.append(th); thead.append(htr); table.append(thead); table.append(tbody); caption.append("Player List"); $('#pList').append(caption); $('#pList').append(table); }); $('#createPlist').on('click', function() { $(this).prop('disabled', true); }); $('#giveTik').click(function() { count = Number($('#noofTiks').val()); for (h = 0; h < count; h++) { var table = $('<table>'); table = $(table).css({ "border": "1px solid black", "margin": "20px auto", "width": "500px", "height": "250px" }) var thead = $('<thead>'); var tbody = $('<tbody>'); for (var i = 0; i < 3; i++) { // append new row to body var row = $('<tr>'); //var row = ''; for (var j = 0; j < 9; j++) { // append new td to row var td = $("<td>"); td = $(td).css({ "border": "1px solid black", }) td.append(""); row.append(td); } tbody.append(row); } var showTable = $('#pList').find('tr').eq(h + 1); //NEED HELP HERE // For "selected" row of table1.. var rowFromTable1 = showTable; //.. Take a clone/copy of it.. var clonedRowFromTable1 = rowFromTable1.clone(); //finding input value var input_value = clonedRowFromTable1.find("input").val(); //getting sr.no var texts = clonedRowFromTable1.find("td:first").text(); //replace td with th and add sr.no and input value clonedRowFromTable1.find("td:first").replaceWith("<th colspan='9'>" + texts + " " + input_value + "</th>") clonedRowFromTable1.find("td:last").remove()//remove next td //append cloned elemnt to thead thead.append(clonedRowFromTable1); table.append(thead)//append to table table.append(tbody); $('#div').append(table); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" id="noofTiks"> <div id="pList"></div> <button class="btn btn-error" id="createPlist">OK</button> <div id="div"></div> <button id="giveTik">CREATE TICKETS</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM