2016-03-01 235 views
0

我引用的变量不断重置为0,为什么?每个部分都有一组Previous和Next按钮,最初它们工作正常,但当我返回到某个部分时,该部分的计数器设置为0.它应该保留之前设置的数字。提前致谢。这不是正在使用的实际代码,但它证明了问题(我希望)jquery使用参考变量

var currentPageIndex = null; 
var section1_count = 0; 
var section2_count = 0; 
var section3_count = 0; 

function checkSectionPage(value){ 
    switch(value){ 
     case "section1": 
      currentPageIndex= section1_count; 
      break; 
     case "section2": 
      currentPageIndex= section2_count; 
      break; 
     case "section3": 
      currentPageIndex= section3_count; 
      break; 
    } 
} 
$('.slidePrevious').click(function(){ 
    checkSectionPage($(this).parent().attr('id')); 
    currentPageIndex--; 
}); 
$('.slideNext').click(function(){ 
    checkSectionPage($(this).parent().attr('id')); 
    currentPageIndex++; 
}); 
+0

,因为你永远不会更新节数。它总是零。 – epascarello

回答

1

您从不更新节#count。当您将currentPageIndex设置为该部分时,部分编号不会增加。您需要手动更新它。

做这样的事情:

var activeSection = "section1"; 
var sects = { 
    "section1" : 0, 
    "section2" : 0, 
    "section3" : 0 
}; 

$('.slidePrevious').click(function(){ 
    sects[$(this).parent().attr('id')]--; 
}); 
$('.slideNext').click(function(){ 
    sects[$(this).parent().attr('id')]--; 
}); 
+0

感谢您的快速回复和解决方案。最受赞赏。 – user6002729

0

你是不是递增/递减你认为你是。
你想要的东西,如:

currentPageIndex = "section1_count"; 
... 

window[currentPageIndex]++; 

(裸瓦尔,“全局变量”,在浏览器方面都在window对象实际上字段)

或您的计数移动到一个对象(像@ pascarello的答案),或者也许排成一个阵列,沿着这些线:

var pageIndexes = [ 0, 0, 0 ]; 

which = 1; 
... 

pageIndexes[which]++;