我需要创建一个变量,并为此变量分配一个值,以供以后使用。为了节省复杂性并保持简单性,我删除了一些细节,这些细节也是var item2其他内容的一部分。在此示例中,变量将为mySeries

var mySeries;

$.getJSON("/_scripts/proxy.php", jsonObj, function(item)
{
//Stuff happens here

//NEED TO ASSIGN VALUE TO mySeries HERE:
mySeries = item[0].series_id;

//Stuff happens here
});

//NEED TO USER mySeries VALUE HERE BUT IT IS NOT DEFINED
var item2 = mySeries;

最佳答案

使用ajax时,您需要使用回调,因为ajax是asynchronous

$.getJSON("/_scripts/proxy.php", jsonObj, function(item)
{
   var mySeries;
   //Stuff happens here

   //NEED TO ASSIGN VALUE TO mySeries HERE:
   mySeries = item[0].series_id;
   save_series(mySeries)
   //Stuff happens here
});

function save_series(s){

   var item2 = mySeries;

   //...etc
}

07-26 05:50