2016-03-20 162 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