2013-08-23 19 views
2

我有一個使用Jquery和datatables插件從後端返回的數據表。我需要選擇數據表中的訂單號並提醒它。警報和控制檯在數據表的第一頁中很好地工作,但不會從第二頁開始。我GOOGLE了它,但.live()已棄用,建議的答案.on()似乎不起作用。Alert()不會從數據表的第二頁觸發

的Jquery:

$(document).ready(function() { 
$.ajax({ 
    type: "POST", 
    //url: "OrderDetail.asmx/HelloWorld", 
    url: "Order.aspx/GetOrder", 
    data: "{'id':'273440'}", 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (msg) { 
     $('#order').append(msg.d); 
     //alert(msg.d); 
     console.log(msg); 
     $('#orderTable').dataTable({ 
      // "sScrollY": "100px", 

      "bAutoWidth": false, 
      "bDeferRender": true 
      //"bPaginate": false 

     }); 
     // Click order number to get the details 
     // Problem is here 
     $('.orderNumber').on('click', function() { 
      var orderNum = $(this).text(); 
      console.log(orderNum); 
     }); 

    }, 
    error: function (xhr, status, error) { 
     // Display a generic error for now. 
     alert("Ajax Error!"); 
    } 
}); 

});

+0

什麼是你想怎麼辦? –

+0

@ AhmedAlaaEl-Din我需要選擇數據表中的訂單號並提醒它。但它不會在數據表的第二頁中工作 – Quentin

回答

3

您設置的onclick事件僅適用於頁面上的元素。這就是爲什麼它只適用於第一頁。只要datatables顯示新的結果,您應該設置onclick事件。這可以通過將onclick監聽器移動到數據表的fnDrawCallback函數來完成。

$('#orderTable').dataTable({ 
    // "sScrollY": "100px", 

    "bAutoWidth": false, 
    "bDeferRender": true, 
    //"bPaginate": false, 

    "fnDrawCallback": function() { 
     $('.orderNumber').on('click', function() { 
      var orderNum = $(this).text(); 
      console.log(orderNum); 
     }); 
    } 
}); 

看到http://datatables.net/ref#fnDrawCallback

相關問題