2017-01-14 15 views
0

我正在從jQuery數據表加載來自mysql數據庫的少量數據。如何使用jQuery數據表使用onChange事件獲取選定的數據?

下面是HTML代碼:

<table id="employee-grid" cellpadding="0" cellspacing="0" border="0" class="display" width="100%"> 
    <thead> 
     <tr> 
      <th>#</th> 
      <th>User Id</th> 
      <th>Address</th> 
      <th>Package Name</th> 
      <th>Status</th> 
      <th>Action</th> 
     </tr> 
    </thead> 
</table> 

jQuery的數據表和我的AJAX請求的jQuery代碼:

$(document).ready(function() { 

    var dataTable = $('#employee-grid').DataTable({ 
     "processing": true, 
     "serverSide": true, 
     "ajax":{ 
      url :"server_processing.php", // json datasource 
      type: "post", // method , by default get 
      error: function(){ // error handling 
       $(".employee-grid-error").html(""); 
       $("#employee-grid").append('<tbody class="employee-grid-error"><tr><th colspan="3">No data found in the server</th></tr></tbody>'); 
       $("#employee-grid_processing").css("display","none"); 

      } 
     } 
    }); 

    $("#employee-grid").on('change', function() { 
     var status = $('.changeStatus').val(); 
     alert(status); 
     $.ajax({ 
      url : "<?php echo SITE_URL . 'toplevel/update-data'; ?>", 
      data : { 
       'statusChange' : status 
      }, 
      type : 'POST', 
     }); 
    }); 

}); 

,但是當我選擇的選項,然後它通過每一次第一個選項值

例如這selected選項標籤有以下值:

<option value='1|11' $statusMsg1>Active</option> 
<option value='2|11' $statusMsg2>Blocked</option> 

它總是通過1|11值!這應該是通過我選擇的選項值。我不明白爲什麼它的發生:(

回答

1

變化

$(".changeStatus").on("change", function(){ 
    var status = $('.changeStatus').val(); 
    alert(status); 

    // e.t.c... 
}); 

$(".changeStatus option").change(function(){ 
    var status = $(this).val(); 

    $.ajax({ 
     url: "<?= SITE_URL . 'toplevel/update-data'; ?>", 
     method: "POST", 
     data: { statusChange : status }, 
     success: function(data){ 
      alert(status); 
     } 
    }); 
}); 

希望它可以幫助

+0

啊..使用它!不調用'Ajax' :( –

+0

@shibbirahmed我沒有看到它不會的原因..控制檯中的任何錯誤? –

+0

在控制檯沒有錯誤 –

相關問題