【发布时间】:2023-03-19 19:15:01
【问题描述】:
我在任何函数之外声明 windows.uris, 然后我在 $.each() 中添加元素,在警报 window.uris 为空时的最后一行。 为什么?
window.uris = new Array();
window.groups = new Array();
jQuery(document).ready(function($) {
host = document.location.host,
path = document.location.pathname;
url = host + path;
var domg = new Object();
var optgroup;
var productsDom = "";
if (contains(url, 'manage/items.php')) {
$.post('http://localhost/frontaccounting/proxy.php',
{
"url":"http://localhost:8081/stock/categories/",
"m":"get",
"data": ""
},
function (data) {
d = $.parseXML(data);
$xml = $( d );
$xml.find("category").each(
function (i,e) {
optgroup = '<optgroup label="'+ $(e).children("name").text()+'">';
categoryId = $(e).children("id").text();
auxx = (categoryId*1);
window.uris[i]="http://localhost:8081/stock/categories/" + (auxx );
window.groups[auxx + ""] = optgroup;
}
);
}
);
//sleep(2000);
alert('URI' + window.uris);
【问题讨论】:
-
你应该在 post callbac 中做警报,这里它在 window.uris 是 feed 之前被调用
-
你的 ajax 是异步的。如果您在
$.post回调中放置另一个警报,您会注意到它在当前警报之后 发出警报。
标签: javascript jquery global-variables scope