带有嵌套 .each() 的 jQuery AJAX 成功函数 - 数据未定义
是的,这可能有点难以解释。
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这不起作用的原因是 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);
types
不是一个 jQuery 对象,它是一个数组,您可以使用each
函数对其进行迭代。对于
types
变量,使用 for 循环或将其转换为 jQuery 对象。types
is not a jQuery object, it is an array, that you can iterate through with theeach
function.For
types
variable use a for loop or convert it to a jQuery object.这并不完全是您具体问题的答案,但您可以使用
map()
将其简化为以下内容:假设 $xml 是一个包含 jQuery 包装的 XML 的变量。例如我在控制台这样定义来测试:
This isn't exactly an answer to your specific question, but you could use
map()
to simplify that down to something stemming from this:That assumes $xml is a variable containing the jQuery-wrapped XML. For example, I defined it like this at the console to test: