我使用 $.ajax 在“for”循环中读取 xml 信息
这是我的 xml 文件:
<application>
<app id="id-1">
<html_code>
<div id="id-1" class="portlet">
<div class="portlet-header"Links</div>
<div class="portlet-content">id-1</div>
</div>
</html_code>
</app>
<app id="id-2">
<html_code>
<div id="id-2" class="portlet">
<div class="portlet-header"Links</div>
<div class="portlet-content">id-2</div>
</div>
</html_code>
</app>
<application>
然后我使用 $.ajax 获取标签中的内容,并在此页面中添加一个包含 这是js代码:
$.ajax({
……
success:function(){
for (var i = 0; i < $children.length; i++) {
var $temp_id = $children.eq(i).attr("id"); //"$children" have defined in above
$.ajax({
type: "get",
url: "Database/App_all.xml",
dataType: "html",
success: function (xml) {
var $temp_code = $(xml).find("app[id='" + $temp_id + "']").find("html_code").html();
$($temp_code).appendTo($("#contain")).hide().show('slow');
},
error: function () { }
});
}
}
});
假设$children.length只有2,那么结果就是包含<div id="contain">应该有 <div class="portlet-content">id-2</div>和 <div class="portlet-content">id-1</div>
但结果是 <div id="contain">只有一种,是<div class="portlet-content">id-2</div>
这是怎么回事?
但是当我写 alert("");在 for (var i = 0; i < $layout_left_children.length; i++) { 之间和 var $temp_id = $layout_left_children.eq(i).attr("id");
喜欢
for (var i = 0; i < $children.length; i++) {
alert($children.eq(i).attr("id")); //could alert correct "id",first "id-1", then "id-2"
var $temp_id = $children.eq(i).attr("id"); //"$children" have defined in above
那么包含可能是对的
那么为什么会这样呢?我该如何解决这个问题?
谢谢你:)
最佳答案
我相信有几件事情正在发生。首先,$temp_id 变量被提升到函数的顶部,所以它相当于这样做:
$.ajax({
……
success:function(){
var $temp_id;
for (var i = 0; i < $children.length; i++) {
$temp_id = $children.eq(i).attr("id");
其次,即使 $temp_id 在第一个循环中等于“id-1”,它在你的第二个循环中被更改为“id-2”,所以当你的成功回调第一次被调用时,它已经被更改为“id-2”。
这应该可以解决您的问题:
2010-12-03 更新:修复了一个错误
$.ajax({
……
success:function(){
for (var i = 0; i < $children.length; i++) {
var $temp_id = $children.eq(i).attr("id"); //"$children" have defined in above
$.ajax({
type: "get",
url: "Database/App_all.xml",
dataType: "html",
success: function($tid) {
return function (xml) {
var $temp_code = $(xml).find("app[id='" + $tid + "']").find("html_code").html();
$($temp_code).appendTo($("#contain")).hide().show('slow');
}
}($temp_id),
error: function () { }
});
}
}
});
我正在做的是将 $temp_id 传递给返回另一个函数的函数,该函数将在成功回调中调用。现在可以安全地在成功回调中使用 $tid,因为当 $temp_id 在第二个循环中更改时它不会受到影响。
更新:对下面 hh54188 评论的回应
使用“警报”可以中断执行过程并允许意外调用 ajax 回调。在 IE 和 Firefox 中就是这种情况。另一方面,Chrome 并不像这样。要对此进行测试,您可以运行此代码:
for (var i = 0; i < 2; i++) {
//if (i == 1) alert('stopping execution');
console.log('loop: ' + i);
$.ajax({
url: "Database/App_all.xml",
dataType: "html",
success: function () {
console.log('callback');
}
});
}
你会看到控制台的输出是:
循环:0
循环:1
回调
回调
现在取消注释带有警报的行。在 Firefox 和 IE 中,您会看到控制台中的输出现在是:
循环:0
回调
循环:1
回调
第一个回调在显示警告框时被调用。警报从根本上改变了代码的行为。在使用 JavaScript 进行开发时,使用“警报”可能会令人沮丧,因为它会导致代码执行不可预测。因此,我不建议在调试时使用“警报”。相反,使用 console.log() 更容易预测。 console.log() 适用于所有现代浏览器和 IE 8+。对于较旧的 IE,您需要将文本输出到 DOM。
关于xml - 关于 "for"循环中jquery$.ajax的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4343048/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
由于fast-stemmer的问题,我很难安装我想要的任何rubygem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test