简体   繁体   English

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

[英]How to add and remove table rows using checkboxes

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 这是jQuery代码

<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 这是PHP代码我正在复选框

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 最后这是我的html代码,其中出现了复选框

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

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

           </tbody>

       </table>
 </div>

I could not figure out what #findings-table is, therefor I just created a table. 我无法弄清楚#findings-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> 

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

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