【发布时间】:2016-03-20 14:23:03
【问题描述】:
如何在同一函数中访问“mentorslist”变量。 “mentorslist”是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
}
看到这里的导师列表变量设置为 ajax 成功并试图通过自定义函数返回它,但它返回我未定义。
【问题讨论】:
标签: javascript php jquery ajax codeigniter