javascript - 如何使用 jQuery DataTables 从所有页面提交复选框

标签 javascript php jquery ajax datatables

我正在尝试获取每一行的第一个单元格 (td) 并获取它,但仅限于当前页面。如果我导航到下一页,则不会发送在上一页选中的复选框。

<table class="table" id="example2">
    <thead><tr>

            <th>Roll no</th><th>Name</th></tr><thead>
        <?php
        $sel = "SELECT * FROM `st`";
        $r = mysqli_query($dbc, $sel);
        while ($fet = mysqli_fetch_array($r)) {
            ?>
            <tr>
                <td><?php echo $fet['trk'] ?></td>
                <td><input type="text" value="<?php echo $fet['ma'] ?>" id="man" class="form-control"></td>
                <td><input type="checkbox" id="check" name="myCheckbox" class="theClass"></td></tr>
        <?php } ?>


</table>

<input type="submit" id="sub_marks" class="btn btn-info" value="Submit & Continue">

<script src="plugins/datatables/jquery.dataTables.min.js" type="text/javascript"></script>
<script src="plugins/datatables/dataTables.bootstrap.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        $('#example2').DataTable({
            "paging": true,
            "lengthChange": false,
            "searching": false,
            "ordering": true,
            "info": true,
            "autoWidth": false,
        })

    });
</script>

<script>


    $('#sub_marks').click(function () {

        var values = $("table #check:checked").map(function () {
            return $(this).closest("tr").find("td:first").text();
        }).get();
        alert(values);
    })
</script>

最佳答案

原因

出于性能原因,jQuery DataTables 会从 DOM 中删除不可见的行。提交表单时,只有可见复选框的数据会发送到服务器。

解决方案 1. 提交表单

你需要转元素<input type="checkbox">已检查但不存在于 DOM 中的 <input type="hidden">提交表格后。

var table = $('#example').DataTable({
   // ... skipped ...
});

$('form').on('submit', function(e){
   var $form = $(this);

   // Iterate over all checkboxes in the table
   table.$('input[type="checkbox"]').each(function(){
      // If checkbox doesn't exist in DOM
      if(!$.contains(document, this)){
         // If checkbox is checked
         if(this.checked){
            // Create a hidden element 
            $form.append(
               $('<input>')
                  .attr('type', 'hidden')
                  .attr('name', this.name)
                  .val(this.value)
            );
         }
      } 
   });          
});

解决方案 2:通过 Ajax 发送数据

var table = $('#example').DataTable({
   // ... skipped ...
});

$('#btn-submit').on('click', function(e){
   e.preventDefault();

   var data = table.$('input[type="checkbox"]').serializeArray();

   // Include extra data if necessary
   // data.push({'name': 'extra_param', 'value': 'extra_value'});

   $.ajax({
      url: '/path/to/your/script.php',
      data: data
   }).done(function(response){
      console.log('Response', response);
   });
});

演示

参见 jQuery DataTables: How to submit all pages form data了解更多详情和演示。

注意事项

  • 每个复选框都应该有一个 value分配有唯一值的属性。
  • 避免使用 id属性 check对于多个元素,此属性应该是唯一的。
  • 您不需要显式启用 paging , info等 jQuery DataTables 的选项,这些选项默认启用。
  • 考虑使用 htmlspecialchars() 函数正确编码 HTML 实体。例如,<?php echo htmlspecialchars($fet['trk']); ?> .

关于javascript - 如何使用 jQuery DataTables 从所有页面提交复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33240409/

相关文章:

php - Jquery链接鼠标悬停平移谷歌地图

javascript - jQuery .append() 没有换行符

javascript - AJAX : Applying effect to CSS class

php - PHP语法初学者,mySQL_query错误

php - 如何将文章存入mysql数据库

php - Mysql查询获取保修产品

javascript - ckeditor如何在图片上传后放置<br>

javascript - iframe 内的 Google map 未加载

javascript - keydown 上的 event.preventDefault() 不起作用

javascript - 在 jquery 代码中的何处插入 PreventDefault