大家好,我有这个代码
var temp;
if(method==1)
temp = $("#Words").val(); //get the words from textbox
else{
$.getJSON("http://localhost/mine/test.js", function(data) {
temp=data.Words;
});
}
//do something with temp...but temp is undefined after the execution of else
但执行 getJson 后 temp 未定义...如果我发出警报(temp),它会继续发生什么?我如何获取 temp 的值来继续?
提前致谢!
这是因为 getJSON 是 ajax 请求,不是同步的。尝试这个
var temp;
if(method==1) {
temp = $("#Words").val(); //get the words from textbox
proc(temp);
} else {
$.getJSON("http://localhost/mine/test.js", function(data) {
temp=data.Words;
proc(temp);
});
}
function proc(data){
//do something with temp...but temp is undefined after the execution of else
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)