2017-08-03 29 views
0

這是問題的html,問題基本上是當我點擊datatable第一頁上的userimage類,然後它工作正常,但是當我點擊datatable的第二頁然後even不被解僱。Jquery不工作在數據表的第二頁

<table class="table" id="example1"> 
    <thead> 
    <tr> 
     <th>S.No.</th> 
     <th>Name</th> 
     <th>Email</th> 
     <th>Contact</th> 
     <th>Action</th> 
    </tr> 
    </thead> 
    <tbody> 
    <?php 
     $i = 0; 
     foreach($users as $user){ 
     $i++; 
     $id = $user['id']; 
     $btn_class = ($user['status'] ==1 ? "btn btn-success" : "btn btn-warning"); 
     $url_attribute = ($user['status'] ==1 ? "user-disable" : "user-enable"); 
     $btn_text = ($user['status'] ==1 ? "Disable" : "Enable"); 
     $picture = $_SERVER['DOCUMENT_ROOT']. "/temp/assets/images/user_profile/". $user['image']; 
     $user_profile_pic = (file_exists($picture) && $user['image'] != "" ? base_url()."/temp/assets/images/user_profile/". $user['image'] : ($user['social_image'] ? $user['social_image'] : "")); 
     ?> 
     <tr> 
      <td><?= $i;?></td> 

這是在其click事件被觸發的userimage類。

  <td <?php if($user_profile_pic != ""){ ?> class="userimage" data-userimage="<?= $user_profile_pic;?>"> 
      <a data-toggle="modal" data-target="#myModal" href="" <?php } ?>> 
      <?= ($user['fulname'] ? $user['fulname'] : "N/A"); ?></a> 
      </td> 
      <td><?= ($user['email'] ? $user['email'] : "N/A");?></td> 
      <td><?= ($user['contact'] ? $user['contact'] : "N/A");?></td> 
      <td> 
      <a href="<?= base_url().'admin/user-view/'.$id;?>" class="btn btn-info">View Detail</a> 
      <a href="<?= base_url().'admin/user-delete/'.$id;?>" class="btn btn-danger" onclick="return confirm_delete();">Delete</a> 
      <a href="<?= base_url().'admin/'.$url_attribute.'/'.$id;?>" class="<?= $btn_class;?>"><?= $btn_text;?></a>  
      </td> 
     </tr> 
     <?php 
     } 
    ?> 
    </tbody> 
</table> 

這是JavaScript代碼。

<script type="text/javascript"> 
    function confirm_delete(){ 
    var x = confirm("Are You Sure, you want to delete this User ?"); 
    if(x){ 
     return true; 
    }else{ 
     return false; 
    } 
    } 

    $("#example1").DataTable(); 

    $(".userimage").click(function(){ 
    var image = $(this).attr('data-userimage'); 
    var name = $(this).text().toUpperCase(); 
    $("#image").show(); 
    $("#image").attr('src',image); 
    $("#fulname").text(name); 
    }) 
</script> 
+0

而不是像這樣粘貼代碼使用小提琴,snippet或plunkr來複制你的問題 –

回答

0

發生這種情況,因爲該元素是不是在DOM。當您使用數據表切換頁面時,它會創建元素。要在數據表中的元素添加事件,你需要這樣創建一個事件:

$("#example1").on("click", ".userimage", function(event) { 
    var image = $(this).attr('data-userimage'); 
    var name = $(this).text().toUpperCase(); 
    $("#image").show(); 
    $("#image").attr('src',image); 
    $("#fulname").text(name); 
}); 

此結合事件表中,但在單擊圖像類時運行的功能。

+0

謝謝y0hami,它真的爲我的問題:) –

相關問題