Javascript 获取历史记录页面的名称

Javascript 获取历史记录页面的名称,javascript,jquery,Javascript,Jquery,我在这里获取每个用户的名称,并将id设置为该名称。我将图像的id设置为此处的名称。然后单击这些图像,我将名称传递到history.php页面,其中显示使用该名称的数据。但是现在在window.location中,行项目未定义。这是正确的方法还是有其他标准方法 $.getJSON('rest.php/names', function(data) { var items = []; var recs = data.records; for(var i=

我在这里获取每个用户的名称,并将id设置为该名称。我将图像的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];
            });
        }
    });
$.getJSON('rest.php/names',函数(数据){
var项目=[];
var recs=数据记录;
对于(var i=0;i更改此行

window.location = 'history3.php?name=' + items[i];
如下所示并检查

window.location = 'history3.php?name=' + $(this).attr('id');
*按照@Neal的建议更新use
this.id
,这样会更有效

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

window.location = 'history3.php?name=' + items[i];
如下所示并检查

window.location = 'history3.php?name=' + $(this).attr('id');
*按照@Neal的建议更新use
this.id
,这样会更有效

window.location = 'history3.php?name=' + this.id;
换成这个

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

换成这个

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


不是问题的解决方案,而是将单击处理程序移出for循环。只需绑定一次事件。不是问题的解决方案,而是将单击处理程序移出for循环。只需绑定一次事件。