javascript - 为什么不在数组中添加元素?

标签 javascript jquery arrays

代码:

$(function(){
    var Name = [];

    for($i=1; $i<16; $i++) {
        var id = $i;
        $.post("./index.php", {
            record : id
        }, function(data){
              Name.push(data);
        });
    }

    alert(Name);
});

数据返回结果为<a href="#"><img src="./name.jpg"></a>

请告诉我为什么数据没有添加到数组中?

最佳答案

post 请求是一个异步方法

甚至在点击成功功能之前,您就会点击警报。

    Asynchronous means that the script will send a request to the
 server, and continue its execution without waiting for the reply.

关于javascript - 为什么不在数组中添加元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14579794/

相关文章:

javascript - 如何使用基于 YUI 的 EditorGridPanel 和 ColumnTree 替代品来避免 EXTjs?

jquery - Testcafe 得到 json 响应

JQuery 模态对话框 - 销毁还是关闭?

arrays - Bash - 如何将列表分配给数组成员

javascript - 如何清理从所见即所得编辑器收到的数据?

javascript - 在 chrome 中捕获 ctrl+n 组合键

javascript - 如何根据前一个元素更新下一个元素值

javascript - 检查某些文本并添加类

javascript - 拆分数组导致错误: not a function

Python 映射数组