0

I have this piece of jquery/javascript and i don't understand why this is happend and how should i fixed it. The tag with the id="group_"+num is loaded with ajax so it seem not being added to the DOM yet , the funny part is if i put one alert in the middle the function works, without that alert nothing happend , ¿ anyone could explain what alert does there and what should i do to get that id in the DOM so jquery could count the length as 1 ? Of course if i get any other id not loaded with ajax , works.

$(document).ready(function(){

    $(function() {
    var hash = window.location.hash;
    var num = hash.replace("#subcat_group_","");
    $('[id*="subcat_group_"]').hide();

            // here the "id" loaded with ajax seems still not loaded.
    var len = $("#group_"+num).length;
           //Here variable "len" still 0 

           /***** with this alert works , just an alert anything inside

           alert(num);

           with this alert works *****/ 

          /*******if i use "len" here still 0 even if alert is before *******/
           if( $("#group_"+num).length === 1){


          /******if i get again for id lenght with alert before , then is 1 ******/              
        if( $("#group_"+num).length === 1){
        var group_name = $("#group_"+num).attr("title");
        swapContentV1(num,group_name);
    }
    $("#subcat_group_"+num).show();
    });

});

thanks.

4

1 回答 1

0

警报迫使脚本等待……等待您的响应和完成 Ajax 调用。没有警觉,你看起来“太早了”——那时 Ajax 还没有完成。

正如 shoogle 在他的评论中建议的那样,您应该将代码放入成功函数中。

于 2013-09-27T05:39:53.240 回答