2016-03-01 233 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]++;