我对JavaScript没有丰富的经验,我在变量范围和jquery中遇到了问题。我有以下结构:
function pass_variables()
{
username = "efcjoe"
response = post_variables(username)
alert(response)
}
function post_variables(username)
{
$.post(
'/path/to/url/',
{
'username': username,
},
function(data)
{
valid = (data != 0) ? true : false
// OPTION 1: If I put return here...
return valid; // ... the alert box in pass_variables says "undefined"
},
"text"
);
// OPTION 2: If I put return here...
return valid; // ... The alert box does not pop up, and Safari debug gives
// me the error: "Can't find variable: valid"
}
我是否缺少那里的东西?我认为valid
应该是一个全局变量,因此选项2应该可以正常工作。我真的不知道选项1.
任何人都可以给我任何建议,让这个工作的最佳途径?
非常感谢。
好恶,少全局变量越好。在这样的情况下,你做错了什么。 – Dykam 2009-08-09 18:25:38