AJAX回调函数内部给全局变量赋值或回调函数之间传值需要注意的问题

现象:选择的父类与子类无法对应上

问题:ajax异步请求造成的
解决方法:
      $(“input[name=”segment[]”]”).click(function(){
$(“#sub_segment_type”).empty();

$(“input[name=”segment[]”]”).each(function(){
if (this.checked == true) {
var segmentId = $(this).val();
var segment_name;
$.ajax({
type:”POST”,
async:false,
url:”<?php echo site_url() ?>/dept_segment/getSegmentOption/” Math.random(),
data:”segId=” segmentId,
success:function(segments){
var segObjects = eval(segments);
segment_name = segObjects[0].name;
}
});

$.ajax({
type:”POST”,
async:false,
url:”<?php echo site_url() ?>/segment_type/getSegmentType/” Math.random(),
data:”segId=” segmentId,
success:function(data){
var dataObjects = eval(data);

for (var i=0;i<dataObjects.length; i ) {
p_html = “<p class=”has-input-tip”><label></label><label style=”width:90px; margin-right:10px;”>” segment_name “-”   dataObjects[i].name “</label><input type=”text” style=”width:290px;” name=”” segmentId “_” dataObjects[i].value “” value=”” /></p>”;
$(p_html).appendTo(“#sub_segment_type”);
}
}
});
}
});
});