2013-07-17 77 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');