[英]My function won't run?
I'm getting tired of firebug telling my vars aren't defined... 我对萤火虫告诉我的var未定义感到厌倦...
I have a button: next. 我有一个按钮:接下来。 When the button is clicked, I want it to load a php page into a div, assigning the php page the variable representing the next page.
当单击按钮时,我希望它将php页面加载到div中,为php页面分配代表下一页的变量。
To do this, I have a variable crntpage that stores the value of the current page. 为此,我有一个变量crntpage来存储当前页面的值。 In order to calculate what the var for the next page must be I have a function called next which calculates the value and returns it.
为了计算下一页的var必须是什么,我有一个名为next的函数,该函数计算值并将其返回。
Let's assume that we are on page 5: 假设我们在第5页上:
javascript javascript
$(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 html
<div id="previous">
<a href=''> <-Previous</a>
</div>
I keep getting an error saying that next isn't defined. 我不断收到错误消息,指出未定义下一个。 My guess is because my nxt function is not receiving the value of last.
我的猜测是因为我的nxt函数未接收到last的值。 What am I doing wrong?
我究竟做错了什么?
What you are trying to do with the nxt
function can be accomplished more idiomatically with Math.min()
: 您尝试使用
nxt
函数执行的nxt
可以使用Math.min()
更惯用地完成:
$('#next').live('click', function() {
crntpage = Math.min(crntpage + 1, last);
$('#content').load('getposts.php?pagenum=' + crntpage);
return false;
});
You should also prefix variable declarations with the var
keyword, so as not to pollute the global namespace. 您还应该在变量声明前添加
var
关键字,以免污染全局名称空间。
Here's the revised code together: 这是修改后的代码:
$(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'];
}
});
});
It looks like you didn't define next
. 看来您没有定义
next
。 (or maybe it is defined in the part of your code that you didn't post here, I don't know) Have you tried this: (或者它可能是在您未在此处发布的代码部分中定义的,我不知道)您是否尝试过:
$(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;
});
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.