JS AJAX范围问题

时间:2015-04-27 01:38:49

标签: javascript ajax

我得到了undefined_content的id,我找不到原因。

我在猜测范围,但我不知道我在哪里错了。

var extraInfoHTML是获取未定义id和rowID未定义变量的点。

   $(function(){
     var rowID;

        $('.entryx').on('click', function(rowID){
            pid = $(this).attr('profileid');
            rowID = $(this).attr('id');

            $('.additionInfo_dropDown').hide();
            $(this).after("<tr class='additionInfo_dropDown'><td colspan=6>"+extraInfoHTML+"</td></tr>");

         populateExtra(rowID , pid);



         //alert(derp);
        });

    var extraInfoHTML = "<div id='"+ rowID +"_content' class='profileInfoDropDown'>"+
                        "DERPINGTONS"+
                        "</div>";               
    });

function populateExtra(rowID , pid) {
    $.ajax({
      type: "POST",
      url: "admin_populate_profile.php",
      data: { rowID : rowID,
              pid: pid
            }
    }).success(function(datareturn) { 
        idx = rowID +'_content';
        $("#"+idx).html(datareturn);
        alert(datareturn);
    });
 }

1 个答案:

答案 0 :(得分:1)

这是因为在extraInfoHTML事件中设置了document.ready。换句话说,在为文本指定任何值之前,文本是连续的并且rowId已评估。您只在rowId点击事件处理程序中为.entryx分配值。

更改订单可能会解决这个问题:

  $(function(){
     var rowID;

     $('.entryx').on('click', function(rowID){
       pid = $(this).attr('profileid');
       rowID = $(this).attr('id');
       var extraInfoHTML = "<div id='"+ rowID +"_content' class='profileInfoDropDown'>"+
                        "DERPINGTONS"+
                        "</div>";    
       $('.additionInfo_dropDown').hide();
       $(this).after("<tr class='additionInfo_dropDown'><td colspan=6>"+extraInfoHTML+"</td></tr>");

         populateExtra(rowID , pid);
         //alert(derp);
        });    
    });
相关问题