简体   繁体   English

同时将一个表行输入数据复制到另一表行

[英]copy one table row input data to another table row simultaneously

I have developed two tables "TABLE1" and "TABLE2". 我已经开发了两个表“ TABLE1”和“ TABLE2”。 I want if i enter subject name in Table 1 then it will automatically copy in Table 2 and if click on "Add new+" button of Table1 to add new row then this change will happen on Table2. 我想如果我在表1中输入主题名称,则它将自动复制到表2中,如果单击表1的“添加新+”按钮以添加新行,则此更改将在表2中发生。

 $("#insert66").click(function () { $("#table66").each(function () { var tds = '<tr>'; jQuery.each($('tr:last td', this), function () { tds += '<td>' + $(this).html() + '</td>'; }); tds += '</tr>'; if ($('tbody', this).length > 0) { $('tbody', this).append(tds); } else { $(this).append(tds); } }); }); 
 Table1:- <table id="table66" class="table table-bordered table-hover"> <input type="button" class="btn green" value="Add New+" id="insert66"></input> <thead> <th>Subject Name</th> </thead> <tbody> <tr> <td> <input type="text" class="form-control subName" name="subName"> </td> </tr> </tbody> </table> Table2:- <table id="tablecourse" class="table table-striped table-hover"> <thead> <tr> <th>Subject Name</th> <th>Campus</th> </tr> </thead> <tbody> <tr> <td> <input type="text" class="form-control subName" name="subName"> </td> <td> <select id="multipleSelectExample4" style="width:100%;" data-placeholder="Select an option"> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> <option value="4">Option 4</option> <option value="5">Option 5</option> </select> </td> </tr> </tbody> </table> 

I have found one logic but bit clueless how to use this 我已经找到一种逻辑,但是如何使用它却一无所知

var counterClass = 0
$("#table66 input").change(function(){

 if($(this).hasClass("completed")) {
// update
 $("." + $(this).attr("refClass")).html($(this).val())
 } else {
 // append/insert

 $(this).addClass("completed").attr("refClass", "refClass" + counterClass)
 counterClass += 1

 $().append()

 }
 })

Try with index() match .Apply the Append function in both table. 尝试使用index() match。在两个表中都应用Append函数。 $('table').each() .First table input value reflect with second table input value with same index match $('table').each() 。第一个表输入值反映与第二个表输入值具有相同的索引匹配

update 更新

3 table function. 3表功能。 Dont forget to new table id in this function see below snippet 不要忘记此功能中的新表ID,请参见以下代码段

 $(document).ready(function(){ $("#insert66").click(function() { $(".table").each(function() { var tds = '<tr>'; jQuery.each($('tr:last td', this), function() { tds += '<td>' + $(this).html() + '</td>'; }); tds += '</tr>'; if ($('tbody', this).length > 0) { $('tbody', this).append(tds); } else { $(this).append(tds); } }); }); $('table').on('input','.subName',function(){ var index =$(this).closest('table').find('input').index(this); $('#tablecourse').find('input[type=text]').eq(index).val($(this).val()) //for second table $('#table3').find('input[type=text]').eq(index).val($(this).val()) //for 3rd table }) }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> Table1:- <table id="table66" class="table table-bordered table-hover"> <input type="button" class="btn green" value="Add New+" id="insert66"></input> <thead> <th>Subject Name</th> </thead> <tbody> <tr> <td> <input type="text" class="form-control subName" name="subName"> </td> </tr> </tbody> </table> Table2:- <table id="tablecourse" class="table table-striped table-hover"> <thead> <tr> <th>Subject Name</th> <th>Campus</th> </tr> </thead> <tbody> <tr> <td> <input type="text" class="form-control subName" name="subName"> </td> <td> <select id="multipleSelectExample4" style="width:100%;" data-placeholder="Select an option"> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> <option value="4">Option 4</option> <option value="5">Option 5</option> </select> </td> </tr> </tbody> </table> Table3:- <table id="table3" class="table table-striped table-hover"> <thead> <tr> <th>Subject Name</th> <th>Campus</th> </tr> </thead> <tbody> <tr> <td> <input type="text" class="form-control subName" name="subName"> </td> <td> <select id="multipleSelectExample4" style="width:100%;" data-placeholder="Select an option"> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> <option value="4">Option 4</option> <option value="5">Option 5</option> </select> </td> </tr> </tbody> </table> 

Try this one. 试试这个。 if you need any help just ask :) 如果您需要任何帮助,请询问:)

I've add a invisible template row. 我添加了一个不可见的模板行。 This will be added when ever you click the Add-Button. 只要您单击“添加”按钮,就会将其添加。 And a function to copy the text from one input field to the other. 具有将文本从一个输入字段复制到另一个输入字段的功能。

 function addRow() { $('#table1').append('<tr>'+$('#table1 tr.template').html()+'</tr>'); $('#table2').append('<tr>'+$('#table2 tr.template').html()+'</tr>'); } function updateTabel(source, id){ var text = $(source).val(); var row = $(source).closest('tr').index(); $($('#'+id+' tr')[row]).find('input.subName').val(text); } 
  .template { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="button" value="Add New" onclick="addRow()" /><br> <br> Table1:- <table id="table1"> <tr> <th>Subject Name</th> </tr> <tr class="template"> <td> <input type="text" class="subName" onKeyUp="updateTabel(this, 'table2')"> </td> </tr> </table> Table2:- <table id="table2"> <tr> <th>Subject Name</th> <th>Campus</th> </tr> <tr class="template"> <td> <input type="text" class="form-control subName" onKeyUp="updateTabel(this, 'table1')"> </td> <td> <select id="multipleSelectExample4" style="width:100%;" data-placeholder="Select an option"> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> <option value="4">Option 4</option> <option value="5">Option 5</option> </select> </td> </tr> </table> 

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

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