2013-07-17 79 views
0

我爲每個用戶獲取名稱,並將該名稱設置爲該名稱。我在這裏設置圖像的ID作爲名稱。然後在點擊這些圖像時,我將名稱傳遞到顯示使用該名稱數據的history.php頁面。但是現在在window.location訂單項中未定義。這是做到這一點的正確方法還是存在其他標準方法?獲取名稱到歷史頁面

$.getJSON('rest.php/names', function(data) { 
     var items = []; 
     var recs = data.records; 
     for(var i=0; i<recs.length; i++){ 
      items[i] = recs[i].name; 
      $('#theImg').append('<img id="' + items[i] + '"' + 'src="images/Arrow-Left.png" />'); 
      $("img").click(function(){ 
       window.location = 'history3.php?name=' + items[i]; 
      }); 
     } 
    }); 
+1

不是你的問題的解決方案,但移動單擊處理出來的for循環。你只需要綁定一次事件。 –

回答

2

改變這一行

window.location = 'history3.php?name=' + items[i]; 

要如下檢查

window.location = 'history3.php?name=' + $(this).attr('id'); 

*更新使用this.id通過@Neal的建議,這將是更有效的

window.location = 'history3.php?name=' + this.id; 
1

更改爲th是

$('img'+items[i]).click(function(){...} 

window.location = 'history3.php?name=' + $(this).attr('id');