dtdr57046 2018-11-25 06:51 采纳率: 100%
浏览 74

如何使用复选框添加和删除表行

The problem is here that I have successfully written the code to add and remove row on checked checkbox but it only works for the first element.
Like when I click on the first element it adds a row and on unchecking it remove that row second time when I check and uncheck the second checkbox it adds a row on checked and removes row on unchecked.

here is jquery code

<script type="text/javascript">
    $(document).ready(
        function () {
            $('.finding').change(
                function () {
                    if ($('.finding').is(':checked')) {
                        var finding = $(this).next('label').text();

                        $('#findings-table').append("<tr><td>"+finding+"</td></tr>");
                    }
                    else {
                        $('#findings-table').empty();
                    }

                });

        });
</script>

here is php code i'm getting checkboxes

if ($checkResult > 0) {


for ($i=0; $i < $checkResult ; $i++) { 

    $result = mysqli_fetch_assoc($query);

    $findings = $result['name'];

    $count = 1 + $i;
 echo"<tr>
        <th scope='row'>$count</th>
        <td scope='col'>
        <div class='input-group'>
          <input class='finding form-check-input' type='checkbox' name='findings[]' value='$findings'>
          <label class='input-label pt-1 px-3'>$findings</label>
        </div>
        </td>
    </tr>";
}

}

and finally here is my html code where checkboxes appear

<div class='table-container' style="height: 21vh;">
       <table class="table table-hover" style="line-height: 7px;">
           <tbody>

              <?php include 'getfindings.php'; ?>

           </tbody>

       </table>
 </div>
  • 写回答

1条回答 默认 最新

  • dtm41506 2018-11-25 09:19
    关注

    I could not figure out what #findings-table is, therefor I just created a table.

    See this:

    $(function() {
      $('.finding').on('change', function(e) {
        var $this = $(e.currentTarget),
          $findingTable = $('#findings-table'),
          labelText = $this.parents('.input-group').find('.input-label').text();
    
    
        if ($this.is(':checked')) {
          $findingTable.append($('<tr/>').append($('<td/>').text(labelText)));
        } else {
          $findingTable.empty();
        }
    
      });
    
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <table id="findings-table"></table>
    <div class='table-container' style="height: 21vh;">
      <table class="table table-hover" style="line-height: 7px;">
        <tbody>
    
          <tr>
            <th scope='row'>1</th>
            <td scope='col'>
              <div class='input-group'>
                <input class='finding form-check-input' type='checkbox' name='findings[]' value='a'>
                <label class='input-label pt-1 px-3'>nice a</label>
              </div>
            </td>
          </tr>
          <tr>
            <th scope='row'>2</th>
            <td scope='col'>
              <div class='input-group'>
                <input class='finding form-check-input' type='checkbox' name='findings[]' value='b'>
                <label class='input-label pt-1 px-3'>nice b</label>
              </div>
            </td>
          </tr>
          <tr>
            <th scope='row'>3</th>
            <td scope='col'>
              <div class='input-group'>
                <input class='finding form-check-input' type='checkbox' name='findings[]' value='c'>
                <label class='input-label pt-1 px-3'>nice c</label>
              </div>
            </td>
          </tr>
          <tr>
            <th scope='row'>4</th>
            <td scope='col'>
              <div class='input-group'>
                <input class='finding form-check-input' type='checkbox' name='findings[]' value='d'>
                <label class='input-label pt-1 px-3'>nice d</label>
              </div>
            </td>
          </tr>
    
        </tbody>
    
      </table>
    </div>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?