带有嵌套 .each() 的 jQuery AJAX 成功函数 - 数据未定义

发布于 2024-10-17 02:00:42 字数 1661 浏览 4 评论 0原文

是的,这可能有点难以解释。

我有一个 jQuery AJAX 请求,它正在读取一个 XML 文件,其布局类似于

<root>
    <nodes>
        <node type="typeOne" name="nodeNameOne" />
        <node type="typeOne" name="nodeNameTwo" />
        <node type="typeTwo" name="nodeNameThree" />
        <node type="typeThree" name="nodeNameFour" />
        <node type="typeFour" name="nodeNameFive" />
    </nodes>

    <otherNodes>
        <otherNode name="nodeNameOne">Some value</otherNode>
        <otherNode name="nodeNameTwo">Some value</otherNode>
    </otherNodes>
</root>

: XML 文件由第三方工具生成。

我想要做的是读取 的 name 属性的值,然后使用它从相应的 中提取值,如果存在的话。

这就是我在成功回调函数中得到的内容:

function ParseFile(data, status, request){
    var types = ['typeOne', 'typeTwo']; //I don't care about all the <node>s

    alert(typeof data); //displays "object"

    types.each(function(entry, index){
        alert(typeof data); //displays "object"
        var typeNodes = jQuery(data).find('node[type="'+entry+'"]');

        typeNodes.each(function(){
            alert(typeof data); //displays "undefined"
            var name = jQuery(this).attr('name');
            /* The data object is undefined once you get inside this loop,
               so the following doesn't work. */
            var otherNode = jQuery(data).find('otherNode[name="'+name+'"]').text();
        });
    });
}

问题是,我似乎无法从第二个 .else() 循环中访问数据变量。即使我在第一个变量 var newData = data; 中声明了一个变量,newData 也是未定义的。

Right, this might be a bit difficult to explain.

I have a jQuery AJAX request that's reading an XML file with a layout similar to this:

<root>
    <nodes>
        <node type="typeOne" name="nodeNameOne" />
        <node type="typeOne" name="nodeNameTwo" />
        <node type="typeTwo" name="nodeNameThree" />
        <node type="typeThree" name="nodeNameFour" />
        <node type="typeFour" name="nodeNameFive" />
    </nodes>

    <otherNodes>
        <otherNode name="nodeNameOne">Some value</otherNode>
        <otherNode name="nodeNameTwo">Some value</otherNode>
    </otherNodes>
</root>

This isn't something I have control over; the XML files are being generated by a third-party tool.

What I want to do is read the value of the <node>s' name attribute, then use that to pull the value out of the corresponding <otherNode>, if it exists.

This is what I've got in my success callback function:

function ParseFile(data, status, request){
    var types = ['typeOne', 'typeTwo']; //I don't care about all the <node>s

    alert(typeof data); //displays "object"

    types.each(function(entry, index){
        alert(typeof data); //displays "object"
        var typeNodes = jQuery(data).find('node[type="'+entry+'"]');

        typeNodes.each(function(){
            alert(typeof data); //displays "undefined"
            var name = jQuery(this).attr('name');
            /* The data object is undefined once you get inside this loop,
               so the following doesn't work. */
            var otherNode = jQuery(data).find('otherNode[name="'+name+'"]').text();
        });
    });
}

The problem is, I can't seem to access the data variable from within the second .else() loop. Even if I declare a variable inside the first, var newData = data;, newData is undefined as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挖个坑埋了你 2024-10-24 02:00:42

这不起作用的原因是 jQuery 函数修改 data 并尝试将其设置为它认为您已传入的 html 片段。但是,您没有将有效的 html 传递到它,因此将其设置为未定义。您需要将数据复制到另一个变量,而不仅仅是设置引用。

尝试执行

var myData = data.substr(0);

The reason this isn't working is that the jQuery function modifies data and tries to set it to the html fragment that it thinks that you've passed in. However, you didn't pass valid html into it, so it sets it to undefined. You need to copy data to another variable, rather than just set a refernce.

Try doing

var myData = data.substr(0);

最佳男配角 2024-10-24 02:00:42
var types = ['typeOne', 'typeTwo']; 

types 不是一个 jQuery 对象,它是一个数组,您可以使用 each 函数对其进行迭代。

// This will not work 

types.each(function(entry, index){
  //... iteration processing
}; 

对于 types 变量,使用 for 循环或将其转换为 jQuery 对象。

var types = ['typeOne', 'typeTwo']; 

types is not a jQuery object, it is an array, that you can iterate through with the each function.

// This will not work 

types.each(function(entry, index){
  //... iteration processing
}; 

For types variable use a for loop or convert it to a jQuery object.

对你而言 2024-10-24 02:00:42

这并不完全是您具体问题的答案,但您可以使用 map() 将其简化为以下内容:

// Returns an array of ["Some value", "Some value", ""]
$xml.find('node[type=typeOne], node[type=typeTwo]').map(function() { 
  return $xml.find('otherNode[name=' + this.getAttribute('name') + ']').text(); 
});

假设 $xml 是一个包含 jQuery 包装的 XML 的变量。例如我在控制台这样定义来测试:

var $xml = $('<root>
  <nodes>
    <node type="typeOne" name="nodeNameOne" />
    <node type="typeOne" name="nodeNameTwo" />
    <node type="typeTwo" name="nodeNameThree" />
    <node type="typeThree" name="nodeNameFour" />
    <node type="typeFour" name="nodeNameFive" />
  </nodes>

  <otherNodes>
    <otherNode name="nodeNameOne">Some value</otherNode>
    <otherNode name="nodeNameTwo">Some value</otherNode>
  </otherNodes>
</root>');

This isn't exactly an answer to your specific question, but you could use map() to simplify that down to something stemming from this:

// Returns an array of ["Some value", "Some value", ""]
$xml.find('node[type=typeOne], node[type=typeTwo]').map(function() { 
  return $xml.find('otherNode[name=' + this.getAttribute('name') + ']').text(); 
});

That assumes $xml is a variable containing the jQuery-wrapped XML. For example, I defined it like this at the console to test:

var $xml = $('<root>
  <nodes>
    <node type="typeOne" name="nodeNameOne" />
    <node type="typeOne" name="nodeNameTwo" />
    <node type="typeTwo" name="nodeNameThree" />
    <node type="typeThree" name="nodeNameFour" />
    <node type="typeFour" name="nodeNameFive" />
  </nodes>

  <otherNodes>
    <otherNode name="nodeNameOne">Some value</otherNode>
    <otherNode name="nodeNameTwo">Some value</otherNode>
  </otherNodes>
</root>');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文