简体   繁体   English

如何自动更新 JQuery 中的数组?

[英]How do I auto-update an array in JQuery?

I am trying to add an item to the end of an array, kind of like an arraylist.我正在尝试将一个项目添加到数组的末尾,有点像 arraylist。 I tried using the.push() but I can't get it working correctly reading from my xml.我尝试使用.push(),但无法从我的 xml 中正常读取。 How could I "auto" update the array in jquery?我如何“自动”更新 jquery 中的数组? (append to the end of it if you want to think of it that way) (如果您想这样想,请附加到它的末尾)

Here is what my jquery looks like at the moment.这是我的 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 xml

<?xml version="1.0" encoding="utf-8" ?>
<gallery>
    <image src="images/gallery/gallery1.jpg"/>
</gallery>

If you need any more information, I'll happily provide more.如果您需要更多信息,我很乐意提供更多信息。

Declare the images array inside the success function of the ajax call to ensure the scope remains correct then your function should work... Declare the images array inside the success function of the ajax call to ensure the scope remains correct then your function should work...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM