AjAX调用和setInterval
我正在尝试添加分页. 我用
I'm trying to add pagination. I use
$(document).ready(function() {
var pageNum = 1;
$(".paginationing").click(function(){
pageNum = $(this).attr("id");
});
setInterval("ajaxd("+pageNum+")",5000);
});
function ajaxd(pageNum) {
var thisuser = $("#thisusern").text();
$.ajax({
type: "GET",
url: "newstitle.php",
data: "user="+thisuser+"&page="+pageNum,
success: function(msg){
$("#edix").html(msg);
}
});
}
发送页码.但是即使我单击页码,它也只发送页码1. 错误在哪里?如何使用setInterval实现AJAX分页以每5秒调用一次页面?
To send the page number.But it sends only page number 1 ,even I click on the page number. Where's the bug?How can I implement AJAX pagination with setInterval to call the page every 5 seconds?
我不确定为什么要使用setInterval进行分页而不是在click事件处理程序中调用ajaxd函数. 我会这样:
Am not sure why are you using setInterval to paginate instead of calling the ajaxd function in the click event handler. I would do it this way:
$(document).ready(function() {
var pageNum = 1;
$(".paginationing").click(function() {
pageNum = $(this).attr("id");
ajaxd(pageNum);
});
});
function ajaxd(pgNo) {
var thisuser = $("#thisusern").text();
$.ajax({
type: "GET",
url: "newstitle.php",
data: "user=" + thisuser + "&page=" + pgNo,
success: function(msg) {
$("#edix").html(msg);
}
});
}
如果您仍然想使用setInterval,请尝试以下操作:
If you still want to use the setInterval try this:
var pageNum = 1;
$(document).ready(function() {
$(".paginationing").click(function() {
pageNum = $(this).attr("id");
});
setInterval(ajaxd, 5000);
});
function ajaxd() {
var pgNo = pageNum;
var thisuser = $("#thisusern").text();
$.ajax({
type: "GET",
url: "newstitle.php",
data: "user=" + thisuser + "&page=" + pgNo,
success: function(msg) {
$("#edix").html(msg);
}
});
}
PS: 另请注意,HTML元素的有效ID不应以数字开头,但我认为您依赖于此.我建议您生成类似"pg-n"的id,其中n是数字,在click事件中,您可以使用
P.S: Also note that a valid id for a HTML element should not start with a number but I think you are relying on that. I would rather advice you to generate the id something like "pg-n" where n is the number and in the click event you can get the page number using
pageNum = $(this).attr("id").replace(/[^0-9]/g, "");