2013-02-23 26 views
0

主圖像下方有上一個和下一個按鈕,有x個照片(在這種情況下爲7),我需要限制要添加的值x的數量,因此它只能在1到x的範圍內(在這種情況下爲1到7)。使用jQuery增加/減少期間限制x值上一個按鈕和下一個按鈕

顯然,當用戶到達X,點擊下一個應該回到1

問題可以看這裏:http://mylandscapes.splintertaeal.com/gardens/regents-park.htm

任何幫助將非常感激。

代碼:

$(document).ready(function() { 
    var el = $('.nr'); 

    function change(amt) { 
    el.text(parseInt(el.text(), 10) + amt); 
    } 

    $('#next').click(function() { 
    change(1); 
    }); 

    $('#prev').click(function() { 
    change(-1); 
    }); 
}); 
+0

請修改您的問題並添加相關代碼。就目前而言,這個問題是一個教科書無建設性的問題 – Alexander 2013-02-23 15:27:01

回答

1

我說你應該完全刪除這些代碼:

$(document).ready(function() { 
    var el = $('.nr'); 
    function change(amt) { 
     el.text(parseInt(el.text(), 10) + amt); 
    } 
    $('#next').click(function() { 
     change(1); 
    }); 
    $('#prev').click(function() { 
     change(-1); 
    }); 
}); 

,而是修改下面的事件處理程序。

var $nr = $(".nr"); 
$('#next, #prev').on('click', function() { 
    var A = this.id == 'next',X=A?T:0,Y=A?0:T,Z=A?N+1:N-1;N=N==X?Y:Z; 
    $("#display").html('<img src="'+$(E[N]).attr('href')+'" />'); 
    $(".display2").html('<img src="'+$(F[N]).attr('href')+'" />'); 
    /* Update the slide number */ 
    $nr.text(N + 1); 
}); 

在這種情況下不需要有兩個事件處理程序。

+0

似乎正在工作,但現在圖像不加載,除非點擊按鈕 – splinterteal 2013-02-23 16:01:06

+0

@splinterteal,那麼你刪除了錯誤的代碼。我編輯我的答案,明確顯示你需要刪除的代碼 – Alexander 2013-02-23 16:04:50

+0

瞭解它,非常感謝! – splinterteal 2013-02-23 16:10:15

0
 int i = 0 
     $(nextbutton).click(function() { 
      i++; 
      if (i == 7) { 
      i = 0; 
      } 
showImage(i); // get i indexed image from array 
     }); 

     $(prevbutton).click(function() { 
      i--; 
      showImage(i); // get i indexed image from array 
      if (i == 0) { 
      return; 
      } 
     }); 

I hope I understood your problem in a right way . 
+0

在next.click中showImage()函數應該是if語句之後,而不是 – uhura 2013-02-23 15:34:46

+0

糟糕!感謝更正@uhura – 2013-02-23 15:37:54

相關問題