2016-03-20 156 views
1

如何在同一功能中訪問「mentorslist」varibale。 「mentorslist」是ajax調用的成功。但是我無法在mentors()函數中訪問它。Ajax成功功能無法在自定義功能中訪問

function mentors(){ 
    var mentorslist = ''; 
    $.ajax({ 
     type: "POST", 
     url: <?php echo '"'.base_url().'index.php/MentorList/'.'"'; ?>, 
     data: { pagelimit: 1,json: "true" }, 
     success: function(msg) 
     { 
      var obj = jQuery.parseJSON(msg); 
      var $mentor_list =""; 
      var mlist = ''; 
      jQuery.each(obj.resset, function(i, val){ 
      mlist = mlist+'<option value="'+val.mentor_Id+'">'+val.Name+'</option>'; 
      $("#mlist").get(0).options[$("#mlist").get(0).options.length] = new Option(val.Name,val.mentor_Id); 
      }); 
     mentorslist = mlist; //Able to access here 
     } 
    }); 

    return mentorslist; // gives undefine error 
} 

請看這裏mentorslist變量設置成ajax成功並試圖通過自定義函數返回它,但它返回給我undefined。

回答

0

因爲Ajax是異步的,所以只有在觸發成功事件時纔有變量值。所以,你可以定義自己的函數,在成功撥打:

 var mentorslist = ''; 

     function mentors(myCallBack) { 
      return $.ajax({ 
       type: "POST", 
       url: "yourPHP", 
       data: {pagelimit: 1, json: "true"}, 
       success: function(msg) { 
        myCallBack(msg); 
       } 
      }); 
     } 

     mentors(function(msg) { 
      var obj = jQuery.parseJSON(msg); 
      var $mentor_list = ""; 
      var mlist = ''; 
      jQuery.each(obj.resset, function (i, val) { 
       mlist = mlist + '<option value="' + val.mentor_Id + '">' + val.Name + '</option>'; 
       $("#mlist").get(0).options[$("#mlist").get(0).options.length] = new Option(val.Name, val.mentor_Id); 
      }); 
      mentorslist = mlist; //Able to access here 
     }); 

你可能總是Ajax調用的異步屬性設置爲false,以便從異步到SYNCHRONUS一個改變Ajax調用任何情況下:

 function mentors(){ 
      var mentorslist = ''; 
      $.ajax({ 
       async: false, 
       type: "POST", 
       url: <?php echo '"'.base_url().'index.php/MentorList/'.'"'; ?>, 
      data: { pagelimit: 1,json: "true" }, 
      success: function(msg) 
      { 
       var obj = jQuery.parseJSON(msg); 
       var $mentor_list =""; 
       var mlist = ''; 
       jQuery.each(obj.resset, function(i, val){ 
        mlist = mlist+'<option value="'+val.mentor_Id+'">'+val.Name+'</option>'; 
        $("#mlist").get(0).options[$("#mlist").get(0).options.length] = new Option(val.Name,val.mentor_Id); 
       }); 
       mentorslist = mlist; //Able to access here 
      } 
     }); 
     return mentorslist; 
     } 
+0

mentorslist using done給undefined – Ni3

+0

它的工作親愛的...你是搖滾..非常感謝你。 – Ni3

0

它們不在同一個範圍內。你不能那樣做。

你可以做的是在函數外部設置一個變量,並在ajax調用成功時設置它的值。之後,你可以玩它。

也使ajax調用同步,以便它完成後,您的腳本將繼續。

mentorlist; 
function mentors(){ 
    mentorslist = ''; 
    $.ajax({ 
     type: "POST", 
     url: <?php echo '"'.base_url().'index.php/MentorList/'.'"'; ?>, 
     data: { pagelimit: 1,json: "true" }, 
     success: function(msg) 
     { 
      var obj = jQuery.parseJSON(msg); 
      var $mentor_list =""; 
      var mlist = ''; 
      jQuery.each(obj.resset, function(i, val){ 
       mlist = mlist+'<option value="'+val.mentor_Id+'">'+val.Name+'</option>'; 
       $("#mlist").get(0).options[$("#mlist").get(0).options.length] = new Option(val.Name,val.mentor_Id); 
      }); 
    mentorslist = mlist; //Able to access here 
    } 
    }); 
} 

function asd() 
{ 
    console.log(mentorlist); 
} 

mentors(); 
asd(); 
+0

你能分享腳本嗎? – Ni3

+0

我在功能外設置了「mentorslist」變量,但仍然無法工作。 – Ni3