2010-06-15 65 views
0

我正在使用ajax通过循环插入一系列信息块。每个块都有一个标题,并在其中默认隐藏长描述。它们像手风琴一样运作,只在所有的区块中一次显示一个描述。操纵由Ajax插入的内容,而不使用回调

问题是打开第一个块的描述。我真的想用 javascript来完成这个工作。是否有可能操纵 创建ajax调用而不使用回调的元素?

<!-- example code--> 
<style> 
    .placeholder, .long_description{ 
    display:none;} 
</style> 
</head><body> 
<script> /* yes, this script is in the body, dont know if it matters */ 
$(document).ready(function() { 
    $(".placeholder").each(function(){ // Use the divs to get the blocks 
     var blockname = $(this).html(); // the contents if the div is the ID for the ajax POST 
     $.post("/service_app/dyn_block",'form='+blockname, function(data){ 
      var divname = '#div_' + blockname; 
      $(divname).after(data); 
      $(this).setupAccrdFnctly(); //not the actual code 
     }); 
    }); 

    /* THIS LINE IS THE PROBLEM LINE, is it possible to reference the code ajax inserted */ 
    /* Display the long description in the first dyn_block */ 
    $(".dyn_block").first().find(".long_description").addClass('active').slideDown('fast'); 
}); 
</script> 
<!-- These lines are generated by PHP --> 
<!-- It is POSSIBLE to display the dyn_blocks --> 
<!-- here but I would really rather not --> 
<div id="div_servicetype" class="placeholder">servicetype</div>  
<div id="div_custtype" class="placeholder">custtype</div>   
<div id="div_custinfo" class="placeholder">custinfo</div>   
<div id="div_businfo" class="placeholder">businfo</div> 
</body> 
+0

你必须使用回调...这是ap rime的例子,为什么它在那里:) – 2010-06-15 00:27:04

回答

0

的问题是,随着AJAX是asynchronus(默认)

$(".dyn_block").first().find(".long_description").addClass('active').slideDown('fast'); 

之前有任何数据都将执行。所以你不能避免回调。

代替投票。如果你想了slideDown第一个当所有的处理完成后,你可以定义下面的函数

var amount = $(".placeholder").size(); 
function ajaxDone() { 
amount--: 
if(amount == 0) { 
    $(".dyn_block").first().find(".long_description").addClass('active').slideDown('fast'); 
} 
} 

所以剧本是这样的

<script> /* yes, this script is in the body, dont know if it matters */ 

    var amount = $(".placeholder").size(); 
    function ajaxDone() { 
     amount--: 
     if(amount == 0) { 
     $(".dyn_block").first().find(".long_description").addClass('active').slideDown('fast'); 
     } 
    } 


     $(document).ready(function() { 
      $(".placeholder").each(function(){ // Use the divs to get the blocks 
       var blockname = $(this).html(); // the contents if the div is the ID for the ajax POST 
       $.post("/service_app/dyn_block",'form='+blockname, function(data){ 
        var divname = '#div_' + blockname; 
        $(divname).after(data); 
        $(this).setupAccrdFnctly(); //not the actual code 
       }); 
      }); 

      /* THIS LINE IS THE PROBLEM LINE, is it possible to reference the code ajax inserted */ 
      /* Display the long description in the first dyn_block */ 

     }); 
     </script> 

编辑:关于你对不知道羯羊它的问题还是不知道你对HTML JavaScript注释,检查this SO question

尼克建议有更好的方法来实现相同的事情(而不是使用ajaxDone函数)

$(document).ajaxStop(function() { 
    $(".dyn_block").first().find(".long_description").addClass('active').slideDown('fast'); 
    $(this).unbind('ajaxStop'); 
}); 
+0

有一个*多*更简单的方式来做到这一点:)像这样:'$(文件).ajaxStop(函数(){$( “dyn_block:第一.long_description”。)addClass( '主动')了slideDown(”快'); $(this).unbind('ajaxStop');});' – 2010-06-15 00:48:53

+0

你是对的!感谢您指出,我将编辑答案 – Lombo 2010-06-15 00:52:16

+0

$(文档).ajaxStop像冠军一样工作!非常感谢你们! – Cody 2010-06-15 15:11:42

0

AJAX的本质是“异步”。这意味着在异步请求发出后执行继续快乐;在AJAX调用完成之前,您希望存在的内容将不会存在。所以当你尝试访问内容时,你不会得到任何东西。

当你需要使用回调,因为当操作完成,你永远无法知道异步操作处理。我不确定为什么你反对使用回调 - 这就是它的原因 - 帮助你处理异步操作。

编辑

如果你async属性设置为false您可以SJAX(同步JAX)。因此,可以按照您的建议进行操作,但是在请求完成之前您将锁定浏览器。

+0

更正在这里,它*是*可能的。你甚至可以锁定浏览器,这正是'async:false;'所做的。 – 2010-06-15 00:31:24

+0

@Nick Craver确实如此。忘了那个。我很少打扰'async:false'。将编辑修复。 – 2010-06-15 00:32:16