今天我們要用ajax做一個分頁:
實現Ajax分頁:
如果可以的話加上查詢條件
<!doctype html><html lang="en"><head> <meta charset="UTF-8" /> <title>Document</title> <script src="jquery-1.11.2.min.js"></script><style type="text/css">.dangqian{ background-color:#69F}</style></head><body><div> <input type="text" id="key" /> <input type="button" value="查詢" id="chaxun" /></div><table width="50%" border="1" bordercolordark="0" bordercolorlight="0"> <tr> <td>代號</td> <td>名稱</td> <td>父級代號</td> </tr> <tbody id="neirong"><!--顯示內容--> </tbody></table><div id="fenyexinxi"></div></body><script type="text/javascript"> var page = 1;//定個變量,當前要顯示的頁 Load();//加載數據 Loadfenyexinxi();//加載分頁信息 //查詢 $("#chaxun").click(function(){ page = 1; Load();//加載數據 Loadfenyexinxi();//加載分頁信息 }) function Load() { var key = $("#key").val();//查詢條件。用戶輸入的內容 $.ajax({ url:"chuli.php", data: { page: page, key: key },//傳2個參數一當前頁2關鍵字也就是查詢條件 type:"POST", dataType:"JSON", success: function(data){//匿名函數 var str = "";//這個將來要放表格顯示的 for(var k in data)//通過K可以獲得每一條數據 {//加載數據完成 str +="<tr><td>"+data[k].AreaCode+"</td><td>"+data[k].AreaName+"</td><td>"+data[k].ParentAreaCode+"</td></tr>";//要顯示的內容,代號、名稱父級代號 } $("#neirong").html(str); } }); }function Loadfenyexinxi()//分頁信息{ var str = ""; var minys = 1;//最小頁數 var maxys = 1;//最大頁數 var key = $("#key").val();//取關鍵字用于傳到zys頁面用 $.ajax({ async:false, type:"POST", url:"fenye.php", data:{key:key}, dataType:"TEXT", success:function(d){ maxys = d; } }); str += "<span style='text-decoration:underline; cursor:pointer'>總共:"+maxys+"頁</span> "; str += "<span id='prev' style='text-decoration:underline; cursor:pointer'>上一頁</span>"; for(var i=page-2;i<page+3;i++) { if(i>=minys && i<=maxys) { if(i==page) { str += "<span style='text-decoration:underline; cursor:pointer' class='dangqian' bs='"+i+"'>"+i+"</span> "; } else { str += "<span style='text-decoration:underline; cursor:pointer' class='list' bs='"+i+"'>"+i+"</span> "; } } } str += "<span style='text-decoration:underline; cursor:pointer' id='next'>下一頁</span>"; $("#fenyexinxi").html(str); $("#prev").click(function(){ page = page-1; if(page<1) { page=1; } Load(); Loadfenyexinxi(); }) $("#next").click(function(){ page = page+1; if(page>maxys) { page=maxys; } Load(); Loadfenyexinxi(); }) $(".list").click(function(){ page = parseInt($(this).attr("bs")); Load(); Loadfenyexinxi(); })}</script></html>
新聞熱點
疑難解答
圖片精選