The blog system encountered a problem when commenting. After the user clicks the "Last Page" link, the information on the last page is automatically retrieved and displayed.
Store the current page in a global variable. At the same time, click the method in the "Last Page" link to call a function to automatically change the page number to the last page, and then call the method to load comments.
I found that the system did not respond when I clicked "Last Page" for the first time. Clicking it again will grab the information on the last page!
To say that the global variable has not changed, it should have been changed, just like the method of loading comments has failed if the page number is changed.
The code is as follows:
var page = 1; //初始化页数为第一页 var str = ""; $(document).ready(function() { lostguest(); //载入评论的方法 $("a#first").click(function() { page = 1; lostguest(); }); $("a#last").click(function() { if (page > 1) { --page; lostguest(); } else { page = 1; alert("已经是第一页了!") } }) $("a#next").click(function() { if (page < pagecount) { ++page; lostguest(); } else { alert("已经是最后一页了!"); } }) $("a#all").click(function() { getpagecount(); lostguest(); }); }) var getpagecount = function() { var type = "GetPageCount"; $.ajax({ url: 'GetCount.ashx?type=' + type, type: "GET", dataType: 'text', beforeSend: function() { }, error: function() { alert('获取系统日志记录数失败'); }, success: function(count) { pages = Number(count); } }) } var lostguest = function() { $.ajax({ url: 'SqlHelper.ashx?page=' + page, type: "GET", dataType: 'json', beforeSend: function() { $("#loading").show(); }, error: function() { alert('获取系统日志失败'); }, success: function(msg) { $("#guest").empty(); if (msg != "0") { var data = msg.log; str = ""; $.each(data, function(i, n) { str += "<p id='xuhao'>序号:" + n.序号 + "发表日期" + n.日期 + "用户名:" + n.操作员 + "</p>"; str += "<p id='content'>内容:" + n.事件 + "</p>"; }); $("#guest").append(str); $("#loading").hide(); } else { alert("0"); } } }) }
Later I found a workaround to achieve this effect. The code is as follows:
var page = 1; var str = ""; var pagecount; //存储总页数 $(document).ready(function() { getpagecount(); //获取总页数的方法 lostguest(); $("a#first").click(function() { page = 1; lostguest(); }); $("a#last").click(function() { if (page > 1) { --page; lostguest(); } else { page = 1; alert("已经是第一页了!") } }) $("a#next").click(function() { if (page < pagecount) { ++page; lostguest(); } else { alert("已经是最后一页了!"); } }) $("a#all").click(function() { page = pagecount; //更新当前页数为总页数 lostguest(); }); }) var getpagecount = function() { var type = "GetPageCount"; $.ajax({ url: 'GetCount.ashx?type=' + type, type: "GET", dataType: 'text', beforeSend: function() { }, error: function() { alert('获取系统日志记录数失败'); }, success: function(count) { pagecount = Number(count); //读取总页数 } }) } var lostguest = function() { $.ajax({ url: 'SqlHelper.ashx?page=' + page, type: "GET", dataType: 'json', beforeSend: function() { $("#loading").show(); }, error: function() { alert('获取系统日志失败'); }, success: function(msg) { $("#guest").empty(); if (msg != "0") { var data = msg.log; str = ""; $.each(data, function(i, n) { str += "<p id='xuhao'>序号:" + n.序号 + "发表日期" + n.日期 + "用户名:" + n.操作员 + "</p>"; str += "<p id='content'>内容:" + n.事件 + "</p>"; }); $("#guest").append(str); $("#loading").hide(); } else { alert("0"); } } }) }
The above is the detailed content of How to use javascript or jquery to modify global variable code examples. For more information, please follow other related articles on the PHP Chinese website!