在For循环中使用$ post返回空白(未定义)

时间:2018-11-10 16:38:17

标签: javascript datatables

我有以下功能,但是在for循环内添加$ post返回未定义。但是console.log()确实显示正确的结果。我不确定为什么会发生这种情况,将不胜感激。

  function renderTeamMembers(data, type, full, meta) {
    var team = data[2].split(",");
    var arrayLength = team.length;
    var team_members = "";
    var array = [];
    for (var i = 0; i < arrayLength; i++) {
      (function(i){
        var user_details = '';
        $.post('scripts/get_user_details.php', { id: team[i] }, function(data2) {
          var user_details = data2;
          console.log(user_details);
          team_members += '<span class="btn btn-success btn-xs">'+team[i]+'</span>';
        });
      })(i);
    }
    return team_members;
  }

0 个答案:

没有答案