日期:2014-05-16  浏览次数:20590 次

用Jquery的$.get方法遇见的问题
$(document).ready(function() {
var html = "<div class='tabbable tabs-left'><ul class='nav nav-tabs'>";
var html_institute = '';
var html_subject = '';
$.get("@{ViewResource.viewInstitute}",function(institute_data){
$.each(institute_data,function(commentIndex, comment){
html_institute += "<li><a href='#tab"+commentIndex+ "'data-toggle='tab'>"+institute_data[commentIndex]+"</a></li>";
var value = institute_data[commentIndex];
//alert(institute_data[commentIndex]);
//alert(html_institute);
//alert(1);---------------------------------1
$.get("@{ViewResource.viewSubject}",{
institute : value
},function(subject_data){
// alert(commentIndex);
//  alert(value);
alert(html_subject);
html_subject += "<div class='tab-pane' id='tab"+commentIndex+"'>";
$.each(subject_data,function(subject_commentIndex, subject_comment){
//alert(subject_data[subject_commentIndex].subject);
html_subject += "<p>"+subject_data[subject_commentIndex].subject+"</p>";
});
html_subject += "</div>"
//alert(html_subject);
});

});
//html += html_institute + "</ul>" + "<div class='tab-content'>" + html_subject +"</div></div>";
//alert(html)
},"json");
 
});


是一个ajax的get请求过来一个数据后,然后把这个数据当做参数,在异步一次获取一个新的信息!但是发现了一个问题,就是第一个each循环会先直接执行完了,然后再执行第二个each。但是在1处加上alert这句后,程序就正常执行了!这是为什么?求大神解答!

------解决方案--------------------
给你个无限分类列表作参考:

//生成分类列表
var s=getSubid(0,data_categroy,0);
$('#form_publish .categroy').append(s);

data_categroy里面是所有父子分类关系.例如
ID title        父的ID
1  webdeveloper 0
2  html         1
3  css          1


function getSubid(cid,data,nbsp){
var rs,s='';