0

好的,伙计们,我正在尝试在getIndividualMap函数中使用该变量,但出现undefined错误。可能是什么问题呢?

 var i; 
    for(i=0;i<=(data.length -1);i++)
    {   
    $('#ResultsId').append("<table border='1'>" + 

    "<tr>" +
    "<th>UserID</th>" +
    "<th>RUNDATE</th>" +
    "<th>NAME</th>" +
    "<th>TIME</th>" +
    "<th>DISTANCE (KM)</th>" +

    "</tr>" +
    "<tr>" + 
    "<td>" + data[i].UserID + "</td>"  + " " + 
    "<td>" + data[i].runDate + "</td>"  + " " + 
    "<td>" + data[i].firstName + "</td>"  + " " + 
    "<td>" + data[i].Time + "</td>"  + " " + 
    "<td>" + data[i].Distance + "</td>" + 
    "<td>" +"<a href='#' onclick= 'getIndividualMap(i)'>"  + data[i].UserID +"</a>" + "</td>" + 
    "</tr>" + 
    "</table>" );
    }


getIndividualMap(){
console.log(i);
}
4

1 回答 1

4

函数参数变量丢失.....

getIndividualMap(i){
console.log(i);
}

在下面的部分

"<td>" +"<a href='#' onclick= 'getIndividualMap(i)'>"  + data[i].UserID +"</a>" + "</td>" +

i 是硬编码的,但它是一个变量

它应该是

"<td>" +"<a href='#' onclick= 'getIndividualMap("+ i + ")'>"  + data[i].UserID +"</a>" + "</td>" +
于 2013-01-16T11:52:52.190 回答