是的,我之前的答案不起作用,因为我没有对您的代码给予任何关注。:)
问题在于匿名函数是一个回调函数-
即getJSON是一个异步操作,它将在某个不确定的时间点返回,因此即使变量的范围不在该匿名函数(即闭包)之外,它也会没有您认为应该的价值:
var studentId = null;j.getJSON(url, data, function(result){ studentId = result.Something;});// studentId is still null right here, because this line // executes before the line that sets its value to result.Something要使用由getJSON调用设置的studentId值执行的任何代码都需要 在 该回调函数内或 在 回调执行 后 发生。



