将行从一个表复制到另一个表而不复制复选框

时间:2016-12-26 06:36:30

标签: javascript html

这是链接https://jsfiddle.net/Palak_js/c8kq2q5d/4/

我能够将行添加到其他表但是,我不希望复选框也被复制,当我设置默认复选框进行检查时,它不能正常工作。有没有办法可以从其他表中删除复选框,默认情况下设置复选框以选中复选框未选中时删除行



$("#vergeTable input:checkbox.chkclass").click(function() {
  if ($(this).is(":checked")) {
    $(this).closest("tr").clone().appendTo("#vergeTable2");
  } else {
    var index = $(this).closest("tr").attr("data-index");
    var findRow = $("#vergeTable2 tr[data-index='" + index + "']");
    findRow.remove();
  }
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table class="pure-table dataTable table-bordered vergeTable rowClick" id="vergeTable" role="grid">
  <thead>
    <tr role="row">
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Navn</th>
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Mandat</th>
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Status</th>
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Regnskapsplikt</th>
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Dato startet</th>
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Dato til</th>
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Velg</th>
    </tr>
  </thead>
  <tbody>
    <tr data-index="1">
      <td>Alfred Psa Asker</td>
      <td>Ivareta personens interesser innenfor det personlige og økonomiske området</td>
      <td>
        Tidligere
      </td>
      <td>Ordinær</td>
      <td>10.07.2013</td>
      <td>01.10.2016</td>
      <td>
        <input type="checkbox" class="chkclass" />
      </td>
    </tr>

    <tr data-index="2">
      <td>Testfirst Testlast</td>
      <td>Ivareta personens interesser innenfor det personlige og økonomiske området</td>
      <td>
        Nåværende
      </td>
      <td>Ordinær</td>
      <td>05.12.2016</td>
      <td></td>
      <td>
        <input type="checkbox" class="chkclass" />
      </td>
    </tr>
  </tbody>
</table>

<br>-----
<br>

<table class="pure-table dataTable table-bordered selectedVergeTable rowClick" id="vergeTable2" role="grid">
  <thead>
    <tr role="row">
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Navn</th>
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Mandat</th>
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Status</th>
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Regnskapsplikt</th>
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Dato startet</th>
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Dato til</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

您应该绑定change事件而不是click,并且需要将其从克隆对象中删除。 change()trigger('change')可用于在页面加载时触发事件处理程序。

//Bind change event
$("#vergeTable input:checkbox.chkclass").change(function() {
    if (this.checked) {
        //Cache cloned object in a variable
        var clone = $(this).closest("tr").clone();
        //Remove checkbox
        clone.find(':checkbox').remove()
            //Append it
        clone.appendTo("#vergeTable2");
    } else {
        var index = $(this).closest("tr").attr("data-index");
        var findRow = $("#vergeTable2 tr[data-index='" + index + "']");
        findRow.remove();
    }
})
.change(); //<==== Trigger on page load 

$("#vergeTable input:checkbox.chkclass").change(function() {
  if (this.checked) {
    //Cache cloned object in a variable
    var clone = $(this).closest("tr").clone();
    //Remove checkbox
    clone.find(':checkbox').remove()
      //Append it
    clone.appendTo("#vergeTable2");
  } else {
    var index = $(this).closest("tr").attr("data-index");
    var findRow = $("#vergeTable2 tr[data-index='" + index + "']");
    findRow.remove();
  }
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table class="pure-table dataTable table-bordered vergeTable rowClick" id="vergeTable" role="grid">
  <thead>
    <tr role="row">
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Navn</th>
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Mandat</th>
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Status</th>
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Regnskapsplikt</th>
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Dato startet</th>
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Dato til</th>
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Velg</th>
    </tr>
  </thead>
  <tbody>
    <tr data-index="1">
      <td>Alfred Psa Asker</td>
      <td>Ivareta personens interesser innenfor det personlige og økonomiske området</td>
      <td>
        Tidligere
      </td>
      <td>Ordinær</td>
      <td>10.07.2013</td>
      <td>01.10.2016</td>
      <td>
        <input type="checkbox" class="chkclass" checked />
      </td>
    </tr>

    <tr data-index="2">
      <td>Testfirst Testlast</td>
      <td>Ivareta personens interesser innenfor det personlige og økonomiske området</td>
      <td>
        Nåværende
      </td>
      <td>Ordinær</td>
      <td>05.12.2016</td>
      <td></td>
      <td>
        <input type="checkbox" class="chkclass" />
      </td>
    </tr>
  </tbody>
</table>

<br>-----
<br>

<table class="pure-table dataTable table-bordered selectedVergeTable rowClick" id="vergeTable2" role="grid">
  <thead>
    <tr role="row">
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Navn</th>
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Mandat</th>
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Status</th>
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Regnskapsplikt</th>
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Dato startet</th>
      <th align="left" class="sorting_disabled" rowspan="1" colspan="1">Dato til</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>

Updated Fiddle