Ajax计数器不起作用

时间:2012-11-28 00:23:37

标签: php ajax arrays

所有

我正在使用this Ajax tutorial,它基本上从数据库中提取一些记录并显示它们。我想用一个按钮来修改代码,该按钮将逐个显示记录而不是一起显示。我想要实现的行为是每次用户单击Show next按钮时获取下一条记录。

为此,我在Ajax函数中构建了一个小计数器,用作索引来决定要打印的数组元素。这不起作用。 **我的问题是:为什么我的柜台不起作用?

这是html和Ajax代码:

<html>
<body>
<script type="text/javascript">
<!-- 
function createRequest() {
  //Returns HttpRequest
    return request;
  }

//My attempt at a counter.  This doesn't work.
var index=null;

function calcIndex(){
  if(index==null){
    index=0;
  }else{
    index += index;
  }
  return index;
}
(.....)

</body>
</html>

1 个答案:

答案 0 :(得分:1)

您的calcIndex函数声明已损坏,它缺少function部分。你想确定要设置index += index吗?这有点奇怪。不仅如此,即使您修复它并保持原样,索引也永远不会超过零:

var index=null;

function calcIndex(){
  if(index==null){
    index=0;            // First call, now index = 0
  }else{
    index += index;     // Second, third, ... nth call: index = 0 + 0
  }
  return index;
}

让我们简化:

var index = 0;
function calcIndex(){
    return index++;   // Returns zero first, then 1, then 2...
}

但等等,那时为什么你需要一个功能呢?相反,你可以简单地做:

var index = 0;
...
//index = calcIndex();  No point here
var queryString = "?age=" + age + "&wpm=" + wpm + "&sex=" + sex + "&index=" + index++;
                                                                              ^^^^^^^^

干杯

相关问题