代码之家  ›  专栏  ›  技术社区  ›  topper1309

Datatables-Jquery在分页中的Datatables中不工作

  •  0
  • topper1309  · 技术社区  · 8 年前

    我试图在点击时提醒一些数据 创建测验 它在第一个页面上工作,但在其他页面上,警报不工作。我已经附上了桌子的图片

    enter image description here

    我试图在google上找到,但在datatables中找不到与此相关的内容。

    HTML代码:

    <table id="post-table" class="table display-table table-striped table-
    bordered" width="100%" cellspacing="0">
    <thead>
    <tr>
      <th>Class</th>
      <th>Published</th>
      <th>Quiz</th>
      <th>Create Quiz</th>
      <th>View Quiz</th>
    </tr>
    </thead>
    <tbody>
    <?php foreach ($allposts as $allpost) { ?>
    <tr>
      <td><?php echo $grades->getGradename($allpost['grade_id']); ?></td>
      <td><?php echo $others->milliToTime($allpost['created_at']); ?></td>
      <td><?php echo $quiz->quizCount($allpost['post_id']); ?></td>
      <td>
        <a href="#" data-post-id='<?php echo $allpost['post_id']; ?>' class="create-quiz"><i class="fa fa-plus-circle" aria-hidden="true"></i></a>
      </td>
      <td><a href="/admin/quiz/allquiz.php?post_id=<?php echo $allpost['post_id'] ?>"><i class="fa fa-eye" aria-hidden="true"></i></a></td>
    </tr>
    <?php } ?>
    </tbody>
    </table>
    

    脚本

    $('#post-table').DataTable({
        "bSort": false
    });
    
    $('.create-quiz').click(function(e){
        e.preventDefault();
        alert("ss");
    });
    
    1 回复  |  直到 8 年前
        1
  •  2
  •   wezzy    8 年前

    您的问题是,在页面加载时,浏览器会执行javascript,并将代码与第一个页面中存在的按钮相关联。但当您更改页面时,您看到的这些元素是新元素,没有任何事件侦听器附加到它们。

    最简单的解决方案是侦听容器元素甚至文档上的事件。类似于:

    $(document).on('click', '.create-quiz', function(){ console.log('here i am'); }
    

    应该有用