Php 具有动态id的数据表';s

Php 具有动态id的数据表';s,php,jquery,datatable,datatables,Php,Jquery,Datatable,Datatables,我正在将数据表插件用于jQuery和HTML、php和MySQL。当我显式地设置表的id时,一切都正常,但是如果我在MySQLresult上循环,jquery无法“理解”表的id。也许有了代码,它就更清晰了 数据表工作正常: <table class="table table-bordered table-striped mb-none" id="datatable-tabletools"> <thead> <tr> <th st

我正在将数据表插件用于
jQuery
和HTML、php和
MySQL
。当我显式地设置表的id时,一切都正常,但是如果我在
MySQL
result上循环,jquery无法“理解”表的id。也许有了代码,它就更清晰了

数据表工作正常:

<table class="table table-bordered table-striped mb-none" id="datatable-tabletools">
  <thead>
    <tr>
      <th style="width: 60px; text-align: center;">Id</th>
      <th style="width: 400px; text-align: center;">Info A</th>
      <th style="width: 390px; text-align: center;">Info B</th>
      <th style="width: 100px; text-align: center;">Actions</th>
    </tr>
  </thead>
  <tbody>
    <!-- tbody dynamically populated -->
  </tbody>
</table>

  <script type="text/javascript">
    $(document).ready(function () {
    $('#datatable-tabletools').DataTable({
     });
   });
 </script>

身份证件
信息A
信息B
行动
$(文档).ready(函数(){
$(“#数据表工具”)。数据表({
});
});
数据表不工作:

<?php
foreach ($sections as $section)
{
   $insc = Inscripciones::getAllInscriptionBySection($section -> getId());
   if (count($insc) > 0)
   {
     ?>
     <h2 style="margin-bottom: 50px;"><?php echo $section->getNombre(); ?></h2>
     <table class="table table-bordered table-striped mb-none" id="datatable-tabletools-<?php echo $section -> getId(); ?>">
       <thead>
         <tr>
           <th style="width: 60px; text-align: center;">Id</th>
           <th style="width: 450px; text-align: center;">Category</th>
           <th style="width: 390px; text-align: center;">Title</th>
           <th style="width: 140; text-align: center;">Usuario</th>
           <th style="width: 100px; text-align: center;">Acciones</th>
         </tr>
       </thead>
       <tbody>
         <!-- tbody also dynamically populated -->
       </tbody>
     </table>
  <?php
  }
}
  ?>



    <script type="text/javascript">
          $(document).ready(function () {
           $('#datatable-tabletools-1').DataTable({
           });
           $('#datatable-tabletools-2').DataTable({
           })
           $('#datatable-tabletools-3').DataTable({
           })
          });
   </script>


我认为您当前的代码没有任何问题。 您可以尝试使用类选择器应用datatable。
$('.table').DataTable({})

我认为您当前的代码没有任何问题。 您可以尝试使用类选择器应用datatable。
$('.table').DataTable({})

嗨@Pramod,我尝试用一个新的、干净的脚本来编写课程,效果很好,所以我想我错过了其他东西……太好了!。只要你有通用的datatable初始化代码,类就可以了。嗨@Pramod,我用一个新的干净的脚本尝试了类,效果很好,所以我想我错过了一些其他的东西……太好了!。只要您有公共的datatable初始化代码,类就可以了。