function sub2(){ alert(datas); } var datas; $(document).ready(function(){ alert("hi"); $.ajax({ url:"/Blog/GetListBlogServlet", dataTypes:"JSON",//数据类型为json格式 contentType: "application/x-www-form-urlencoded; charset=utf-8", type:"post", data: { }, async:false, success:function(data){ alert(data); datas=eval(data); $("#demoContent").html(datas[0].title); return datas; } , error:function(){ alert("error"); }, statusCode: {404: function() { alert('page not found'); } } }); }); var options={ "id":"page",//显示页码的元素 "data":datas,//显示数据 "maxshowpageitem":3,//最多显示的页码个数 "pagelistcount":2,//每页显示数据个数 "callBack":function(result){ var cHtml=""; for(var i=0;i<result.length;i++){ cHtml+="<li>"+ result[i].title+"</li>";//处理数据 } $("#demoContent").html(cHtml);//将数据增加到页面中 } }; page.init(datas.length,1,options);
通过按钮测试alert(),可以获取到数据了,可是插件无反应,可是如果用此demo自带的datas又可以运行了,不知什么原因,如果将var options和page.init()放入回调函数 那么插件生效,也可判断datas.length的大小而生成页数,可是无法执行 添加数据操作,怎么回事呢???
var datas=""; $(document).ready(function(){ alert("hi"); $.ajax({ url:"/Blog/GetListBlogServlet", dataTypes:"JSON",//数据类型为json格式 type:"post", data: { }, async:false, success:function(data){ alert(data); datas=eval(data); // $("#demoContent").html(datas[0].title); } , error:function(){ alert("error"); }, statusCode: {404: function() { alert('page not found'); } } }); }); var i=0; function sub2(){ alert(datas); page.init(datas.length,1,options); } var options={ "id":"page",//显示页码的元素 "data":datas,//显示数据 "maxshowpageitem":3,//最多显示的页码个数 "pagelistcount":1,//每页显示数据个数 "callBack":function(result){ $("#demoContent").html(datas[i].title);//将数据增加到页面中 i++; } };
我改成这样,完美完成!
var datas; function sub2(){ alert(datas); datas=eval(datas); return datas; } $(document).ready(function sub(){ alert("hi"); $.ajax({ dataTypes:"JSON",//数据类型为json格式 contentType: "application/x-www-form-urlencoded; charset=utf-8", type:"GET", data:null, async:false, url:"/Blog/GetListBlogServlet", success:function(data){ alert(data); datas=data; } , error:function(){ alert("error"); }, statusCode: {404: function() { alert('page not found'); } } }); }); var options={ "id":"page",//显示页码的元素 "data":datas,//显示数据 "maxshowpageitem":3,//最多显示的页码个数 "pagelistcount":2,//每页显示数据个数 "callBack":function(result){ var cHtml=""; for(var i=0;i<result.length;i++){ cHtml+="<li>"+ result[i].title+"</li>";//处理数据 } $("#demoContent").html(cHtml);//将数据增加到页面中 } }; page.init(datas.length,1,options); 后台[{"b_content":"000000000000000000000000000","ca_id":101,"create_time":"12312","id":1,"title":"标题2133333333333333"},{"b_content":"000000000000000000000000000","ca_id":101,"create_time":"12312","id":1,"title":"标题2133333333333333"}]
datas定义成全局变量了,页面没反应
点击下一页的数据就用不了
怎么可以让这些居中显示呀,我加一个<div align="center">不行的呀
ajax 数据不能保存为全局变量 我怎么把数据填到那个datas里去呢
楼主代码放在ajax里面报错 Error: options is not defined
我的页数、和每页的 个数是后台给我的, 我用你这个插件,都控制不了,还不分页 直接都出来了
不能一个页面多用 o(?□?)o
<script type="text/javascript"> function changes() { var datas = ""; $.ajax({ type: "post", url: "/Page/Index", dataType: "JSON", data: { }, success: function (result) { alert(result.str); datas = result; alert(datas.length); }, error: function (jqXHR) { alert("添加失败!"); } }); var options = { "id": "page", //显示页码的元素 "data": datas, //需要显示的数据 "maxshowpageitem": 3, //最多显示的页码个数 "pagelistcount": 2, //每页显示数据个数 "callBack": function (result) { var cHtml = "<table>"; for (var i = 0; i < result.length; i++) { cHtml += "<tr><td>" + result[i].name + "</td></tr>";//处理数据 } cHtml += "</table>"; $("#demoContent").html(cHtml);//将数据增加到页面中 } }; //只需设置options参数,即可使用插件 //插件初始化 } page.init(datas.length, 1, options); </script>
要怎么弄 请指点