Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在JQuery中自动更新数组?_Javascript_Jquery_Arrays_Push - Fatal编程技术网

Javascript 如何在JQuery中自动更新数组?

Javascript 如何在JQuery中自动更新数组?,javascript,jquery,arrays,push,Javascript,Jquery,Arrays,Push,我试图在数组的末尾添加一个项,有点像arraylist。我尝试使用.push()函数,但无法从xml中正确读取它。如何在jquery中“自动”更新数组?(如果你想这样想的话,可以加在后面) 下面是我的jquery目前的样子 images = new Array(); $.ajax({ type: "GET", url: strXMLURL, dataType: "xml", succe

我试图在数组的末尾添加一个项,有点像arraylist。我尝试使用.push()函数,但无法从xml中正确读取它。如何在jquery中“自动”更新数组?(如果你想这样想的话,可以加在后面)

下面是我的jquery目前的样子

images = new Array();
        $.ajax({
            type: "GET",
            url: strXMLURL,
            dataType: "xml",
            success: function(xml) {
                $(xml).find("image").each(function() {
                    images.push($(this).attr("src"));
                });
            },
            error: function() {
                alert("Error reading the XML"); 
            }
        });
xml



如果您需要更多信息,我很乐意提供更多信息。

在ajax调用的success函数中声明images数组以确保范围保持正确,那么您的函数应该可以工作…

如果您使用console.log($(this)),它是否实际显示了您认为拥有的XML节点?如果您有选择的话,您的web服务使用JSON响应时的摩擦通常会更小,因为它已经是javascript对象的格式,因此很容易使用(我认为)如果我得到了正确的数据,我只需要不断地将其添加到数组中。您的代码看起来很好。我会确保你的“images”变量的作用域正确,没有在其他地方被清除/修改,并通过success函数进行调试;images=loadGalleryImages()loadGalleryImages()返回一个数组。如果您发布更多代码,它会有所帮助。
<?xml version="1.0" encoding="utf-8" ?>
<gallery>
    <image src="images/gallery/gallery1.jpg"/>
</gallery>