Javascript 我的功能赢得了';我跑不了?
我厌倦了firebug告诉我的VAR没有定义 我有一个按钮:下一个。单击按钮时,我希望它将一个php页面加载到一个div中,为php页面分配代表下一个页面的变量 为此,我有一个变量crntpage来存储当前页面的值。为了计算下一页的var必须是什么,我有一个名为next的函数,它计算值并返回它 假设我们在第5页: javascriptJavascript 我的功能赢得了';我跑不了?,javascript,jquery,ajax,Javascript,Jquery,Ajax,我厌倦了firebug告诉我的VAR没有定义 我有一个按钮:下一个。单击按钮时,我希望它将一个php页面加载到一个div中,为php页面分配代表下一个页面的变量 为此,我有一个变量crntpage来存储当前页面的值。为了计算下一页的var必须是什么,我有一个名为next的函数,它计算值并返回它 假设我们在第5页: javascript $(document).ready(function() { $.ajax({ url: 'pagination.php', type: 'PO
$(document).ready(function() {
$.ajax({
url: 'pagination.php',
type: 'POST',
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function (pages) {
last = pages['last'];
crntpage = 1;
function nxt(b) {
if (b == last) {
next = last;
} else {
next = b + 1;
}
return next;
}
$('#next').live('click', function() {
crntpage(next);
$('#content').load('getposts.php?pagenum=' + nxt(crntpage));
return false;
});
}
});
});
html
您试图使用nxt
函数执行的操作可以通过Math.min()
更惯用地完成:
您还应该使用var
关键字作为变量声明的前缀,以免污染全局命名空间
以下是修订后的代码:
$(function() {
var currentPage = 1;
var last = 1;
$('#next').live('click', function() {
currentPage = Math.min(currentPage + 1, last);
$('#content').load('getposts.php?pagenum=' + currentPage);
return false;
});
$.ajax({
url: 'pagination.php',
type: 'POST',
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
success: function (pages) {
last = pages['last'];
}
});
});
看起来您没有定义下一步。(或者可能是在你的代码中定义的,你没有在这里发布,我不知道)你试过这个吗:
$(document).ready(function() {
var next = ''; //defining the variable first, then you set it below in nxt()
$.ajax({
url: 'pagination.php',
type: 'POST',
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function (pages) {
last = pages['last'];
crntpage = 1;
function nxt(b) {
if (b == last) {
next = last;
} else {
next = b + 1;
}
return next;
}
$('#next').live('click', function() {
crntpage(next);
$('#content').load('getposts.php?pagenum=' + nxt(crntpage));
return false;
});
}
});
});
您可以使用firebug的调试器设置断点并逐步完成代码。顺便说一句,我看不到您在nxt()
函数的上下文之外定义next
。crntpage(next)
如何工作?您应该将live
从success回调中取出。那样会给你带来麻烦。。。像许多对单个点击的响应一样,他希望如果(b==last)next=last代码>和模数不给我这个!!我知道您只是基于OP的问题,但是对于OP:。live()
在jQuery 1.7中已经被弃用了。如果您使用的是1.7+,则应该使用.on()
。如果必须的话,您仍然可以委托文档作为侦听器,但我打赌您可以找到一个更接近#next
的祖先。
$(function() {
var currentPage = 1;
var last = 1;
$('#next').live('click', function() {
currentPage = Math.min(currentPage + 1, last);
$('#content').load('getposts.php?pagenum=' + currentPage);
return false;
});
$.ajax({
url: 'pagination.php',
type: 'POST',
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
success: function (pages) {
last = pages['last'];
}
});
});
$(document).ready(function() {
var next = ''; //defining the variable first, then you set it below in nxt()
$.ajax({
url: 'pagination.php',
type: 'POST',
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function (pages) {
last = pages['last'];
crntpage = 1;
function nxt(b) {
if (b == last) {
next = last;
} else {
next = b + 1;
}
return next;
}
$('#next').live('click', function() {
crntpage(next);
$('#content').load('getposts.php?pagenum=' + nxt(crntpage));
return false;
});
}
});
});