javascript - 如何使用按钮再次附加同一个表以及如何在另一个表中对表的数据进行行

标签 javascript jquery html css html-table

我开发了一种表格,我在其中使用一些表格来获取数据,其中与这些表格相关的一些关键点是:-

  1. “AddNew2”按钮用于添加一个表,如表 2 。如果我单击表 1 的“AddNew1”按钮,表 2 和所有其他使用 Addnew2 按钮添加的表中的行数将会增加。

  2. 表 1 是获取相同员工姓名的主表是所有表。这意味着如果我单击“AddNew1”按钮,那么表 1、表 2 和其他表中将添加新行,我在表 1 中输入的任何员工姓名都将复制到表 2 和其他表中。

但是我面临着一些问题:-

  1. 无论我在表 1 中输入什么员工姓名,它都会完美地出现在表 2 中,但它不会出现在表 3 和表 4 中(我通过单击 AddNew2 按钮添加的。)

  2. 如果我单击 AddNew1 按钮在表 1 中添加新行,那么表 2 中会增加一行(假设现在表 1 和表 2 有 2-2 行)。但是现在,如果我单击 AddNew2 按钮添加与表 2 相同的表,那么该表将只包含一个表。

我无法解决这个问题。我也在这里添加了我的代码,请看一下。

$(document).ready(function() {
  $("#insert66").click(function() {
    $(".copySubName tbody").append('<tr> <td> <input type="text" class="form-control EmpName" name="EmpName"> </td></tr>')
    $("#tableboth tbody").append('<tr> <td> <input type="text" class="form-control EmpName" disabled="true" name="EmpName"> </td> <td> <input type="text" class="form-control years allownumericwithoutdecimal" maxlength="3" name="years"> </td> <td> <input type="checkbox" id="mandatorySub"> </td> </tr>')
  });
  $('.copySubName').on('input', '.EmpName', function() {
    var index = $(this).closest('table').find('input').index(this);
    //for second table
    $('#tableboth').find('.EmpName').eq(index).val($(this).val())
    //for 3rd table
  });
});


$("#insert17").click(function() {
  $(".individualSalSection").append(' <div class="portlet-body individual individualMarksSectionSub"><label class="label1 col-md-12 individual labelBoardName" style="display:none"> Enter Board Name </label><input type="text" class="form-control individual boardName"></input> <table id="tableboth" class="arrSubjects table table-striped table-hover arrSubjects individual"> <thead> <th>Employee</th> <th> Marks</th> <th> is mandatory</th> </thead> <tbody> <tr> <td> <input type="text" class="form-control EmpName" disabled="true" name="subject1"> </td> <td> <input type="text" class="form-control years allownumericwithoutdecimal" maxlength="3" name="years"> </td> <td> <input type="checkbox" id="mandatorySub"> </td> </tr> </tbody></table></div>')
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Table 1:-

<table id="table66" class="table table-bordered table-hover copySubName">
  <input type="button" class="btn green" value="Add New+" id="insert66"></input>
  <thead>

    <th>Employee Name</th>


  </thead>
  <tbody>
    <tr>

      <td>
        <input type="text" class="form-control EmpName" name="EmpName">
      </td>


    </tr>
  </tbody>
</table>



<div class="portlet light portlet-fit box individual individualSalSection">
  <input type="button" class="btn green individual" value="Add New+" id="insert17"></input>
  <div class="portlet-body individual individualSalSectionSub">
    <label class="label1 col-md-12 individual labelBoardName" style="display:none"> Enter Board Name </label>
    <input type="text" class="form-control individual boardName"></input>
    Table2:
    <table id="tableboth" class="arrSubjects table table-striped table-hover arrSubjects individual">

      <thead>
        <th>Employee</th>
        <th> Marks</th>
        <th> is mandatory</th>
      </thead>
      <tbody>
        <tr>
          <td>
            <input type="text" class="form-control EmpName" disabled="true" name="EmpName">
          </td>
          <td>
            <input type="text" class="form-control years allownumericwithoutdecimal" maxlength="3" name="years">
          </td>
          <td>
            <input type="checkbox" id="mandatorySub">
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

最佳答案

由于您的问题不清楚,所以我不确定正确的解决方案应该是什么,对于您来说,请尝试下面的代码并检查您是否正在寻找这个。我还添加了每种方法以在新表中添加员工姓名 -

Fiddle

$(document).ready(function() {
  $("#insert66").click(function() {
    $(".copySubName tbody").append('<tr> <td> <input type="text" class="form-control EmpName" name="EmpName"> </td></tr>')
    $("#tableboth tbody").append('<tr> <td> <input type="text" class="form-control EmpName" disabled="true" name="EmpName"> </td> <td> <input type="text" class="form-control years allownumericwithoutdecimal" maxlength="3" name="years"> </td> <td> <input type="checkbox" id="mandatorySub"> </td> </tr>')
  });
  $('.copySubName').on('input', '.EmpName', function() {
    var index = $(this).closest('table').find('input').index(this);
    var that = $(this);
  
    //for second table
    $('.tableboth').each(function(index,obj){
    
    $(obj).find('.EmpName').val(that.val())})
    //for 3rd table
  });
});


$("#insert17").click(function() {
  $(".individualSalSection").append(' <div class="portlet-body individual individualMarksSectionSub"><label class="label1 col-md-12 individual labelBoardName" style="display:none"> Enter Board Name </label> <table class="tableboth" class="arrSubjects table table-striped table-hover arrSubjects individual"> <thead> <th>Subject</th> <th> Marks</th> <th> is mandatory</th> </thead> <tbody> <tr> <td> <input type="text" class="form-control EmpName" disabled="true" name="subject1"> </td> <td> <input type="text" class="form-control years allownumericwithoutdecimal" maxlength="3" name="years"> </td> <td> <input type="checkbox" id="mandatorySub"> </td> </tr> </tbody></table></div>')
});
Table 1:-

<table id="table66" class="table table-bordered table-hover copySubName">
  <input type="button" class="btn green" value="Add New+" id="insert66"></input>
  <thead>

    <th>Employee Name</th>


  </thead>
  <tbody>
    <tr>

      <td>
        <input type="text" class="form-control EmpName" name="EmpName">
      </td>


    </tr>
  </tbody>
</table>



<div class="portlet light portlet-fit box individual individualSalSection">
  <input type="button" class="btn green individual" value="Add New+" id="insert17"></input>
  <div class="portlet-body individual individualSalSectionSub">
    <label class="label1 col-md-12 individual labelBoardName" style="display:none"> Enter Board Name </label>
    <input type="text" class="form-control individual boardName"></input>
    Table2:
    <table class="tableboth arrSubjects table table-striped table-hover arrSubjects individual">

      <thead>
        <th>Subject</th>
        <th> Marks</th>
        <th> is mandatory</th>
      </thead>
      <tbody>
        <tr>
          <td>
            <input type="text" class="form-control EmpName" disabled="true" name="EmpName">
          </td>
          <td>
            <input type="text" class="form-control years allownumericwithoutdecimal" maxlength="3" name="years">
          </td>
          <td>
            <input type="checkbox" id="mandatorySub">
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

关于javascript - 如何使用按钮再次附加同一个表以及如何在另一个表中对表的数据进行行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42549033/

相关文章:

javascript - Highcharts - 绘制在 x 轴上曲折的线条时,系列工具提示不起作用

jquery - 在jqGrid的Edit Url中传递参数进行表单编辑

PHP for 循环 - 在表中显示数据

c# - 用于验证密码的 JavaScript

javascript - jQuery 将两个总数相加

javascript - 最大化打印宽度 CSS3

javascript - 如何在叠加层上放置文本?

javascript - 单页应用javascript不卸载会不会导致内存问题

javascript向导/助手插件

html - 如果多个不同的 HTML 元素是不同的元素,它们可以具有相同的 ID 吗?