Home  >  Article  >  Web Front-end  >  How to use javascript or jquery to modify global variable code examples

How to use javascript or jquery to modify global variable code examples

伊谢尔伦
伊谢尔伦Original
2017-07-18 17:05:541636browse

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: &#39;GetCount.ashx?type=&#39; + type, 
type: "GET", 
dataType: &#39;text&#39;, 
beforeSend: function() { 
}, 
error: function() { 
alert(&#39;获取系统日志记录数失败&#39;); 
}, 
success: function(count) { 
pages = Number(count); 
} 
}) 
} 
var lostguest = function() { 
$.ajax({ 
url: &#39;SqlHelper.ashx?page=&#39; + page, 
type: "GET", 
dataType: &#39;json&#39;, 
beforeSend: function() { 
$("#loading").show(); 
}, 
error: function() { 
alert(&#39;获取系统日志失败&#39;); 
}, 
success: function(msg) { 
$("#guest").empty(); 
if (msg != "0") { 
var data = msg.log; 
str = ""; 
$.each(data, function(i, n) { 
str += "<p id=&#39;xuhao&#39;>序号:" + n.序号 + "发表日期" + n.日期 + "用户名:" + n.操作员 + "</p>"; 
str += "<p id=&#39;content&#39;>内容:" + 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: &#39;GetCount.ashx?type=&#39; + type, 
type: "GET", 
dataType: &#39;text&#39;, 
beforeSend: function() { 
}, 
error: function() { 
alert(&#39;获取系统日志记录数失败&#39;); 
}, 
success: function(count) { 
pagecount = Number(count); //读取总页数 
} 
}) 
} 
var lostguest = function() { 
$.ajax({ 
url: &#39;SqlHelper.ashx?page=&#39; + page, 
type: "GET", 
dataType: &#39;json&#39;, 
beforeSend: function() { 
$("#loading").show(); 
}, 
error: function() { 
alert(&#39;获取系统日志失败&#39;); 
}, 
success: function(msg) { 
$("#guest").empty(); 
if (msg != "0") { 
var data = msg.log; 
str = ""; 
$.each(data, function(i, n) { 
str += "<p id=&#39;xuhao&#39;>序号:" + n.序号 + "发表日期" + n.日期 + "用户名:" + n.操作员 + "</p>"; 
str += "<p id=&#39;content&#39;>内容:" + 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn