在javascript代码中访问if块之外的变量值的问题

$("#submit").click(function(){ var query=getquerystring() ; //get the query string entered by user // get the JSON response from solr server var newquery=query; $.getJSON("http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=100&q="+query+"&json.wrf=?", function(result){ //$.each(result.response.docs, function(result){ if(result.response.numFound==0) { $.getJSON("http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=100&q="+query+"&spellcheck=true&json.wrf=?", function(result){ $.each(result.spellcheck.suggestions, function(i,item){ newquery=item.suggestion; }); }); } 

在上面的javascript代码中,一个变量newquery initialy具有查询值。 但当if条件为真时,其值已经改变。 但我的问题是我没有得到if块之外的更改值,而我想要这个更改的值。 我怎样才能做到这一点。

请回复。

编辑

我可以这样做:

  $.getJSON("http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=100&q="+query+"&json.wrf=?" { async:false }, function(result) { 

问题是$ .getJson是异步的。

请参阅: 如何下载文本文件并在jQuery中以字符串forms存储