2011-06-07 38 views
0

我有一個頁面有一個下拉框。在選擇時,根據創建的html表的值,將值發送到php腳本(Ajax)併發送回responseText。該表格被輸出到HTML頁面。我希望表具有可排序的列,所以我已經使用了jQuery數據表,但它不起作用。Ajax下拉式php表排序

我已經將精確的表格剪切並粘貼到html中,然後運行該頁面,然後進行排序工作。

請誰能幫助/建議解決這個問題?

<a div id="txtHint"> <table id="example"> <div> 

這裏是代碼的HTML頁面上的其餘部分:

<script type="text/javascript" src="/js/jquery-1.5.1.js"></script> 
<script type="text/javascript" src="/js/jquery.dataTables.js"></script> 
<script type="text/javascript" charset="utf-8"> 
$(document).ready(function() { 
    $('table#example').dataTable({ 
     "sPaginationType": "full_numbers" 
    }); 
}); 
</script> 

<script type="text/javascript"> 
function selMetal(str,str2){ 
    if (str==""){ 
    document.getElementById("txtHint").innerHTML=""; 
     return; 
    } 
    if (window.XMLHttpRequest){ 
     // code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp=new XMLHttpRequest(); 
    }else{ 
     // code for IE6, IE5 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function(){ 
     if (xmlhttp.readyState==4 && xmlhttp.status==200){ 
      document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 
     } 
    } 
    xmlhttp.open("GET","sql.php?m="+str+"&s="+str2,true); 
    xmlhttp.send(); 
} 
</script> 

回答

0

凡被定義#示例

注意從PHP輸出的表其間輸出?你是否在ajax中重新創建整個表的html?

如果是這樣,你可能會覆蓋jquery dataTable已經處理的#example dom元素。嘗試在設置表html之後重新實例化數據表,例如:

 
xmlhttp.onreadystatechange=function(){ 
     if (xmlhttp.readyState==4 && xmlhttp.status==200){ 
      document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 
      $('table#example').dataTable().fnDestroy(); // might need to destroy the old one first - not sure 
      $('table#example').dataTable({ "sPaginationType": "full_numbers" }); 
     } 
    } 
+0

您傳說中的Mike,非常感謝您的工作。我需要重新啓動#example元素。 :) – Dino 2011-06-07 23:48:58