5 jquery
$(document).ready(function(){
//global vars
var name = $("#username");
var email = $("#email");
function usernameExists() {
$.get("register.php",{ check: 1, username: name.val(), email: email.val() } ,function(m) {
if(m==1) {
return false;
} else {
return true;
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
Firebug在调用此函数时显示正确的响应,但它不返回任何内容......(此$ .get(...)函数已在函数usernameExists()之外进行了测试,但没有返回,并且它完美地工作).
有什么问题以及如何解决?
$(document).ready(function(){
//global vars
var form = $("#register");
var name = $("#username");
var email = $("#email");
$.get("register.php",
{ check: 1, username: name.val(), email: email.val() },
// Have this callback take care of the rest of the submit()
function(m) {
if(m==1) {
form.submit(function(){ return false; });
} else {
form.submit(function(){
if(validateName() & validateEmail() & validatePass1() & validatePass2())
return true
else
return false;
});
}
}
);
function validateName(){
// some check here
}
// and other functions
});
Run Code Online (Sandbox Code Playgroud)
您正在调用的函数不会返回任何内容.
即使它确实试图从你的响应中返回响应$.get(),它也不会起作用,因为调用是异步的,所以当收到响应时,任何使用返回值的代码都可能已经执行了.
您需要做的是从$.get()回调中调用您的代码.
function usernameExists() {
$.get("register.php",{ check: 1, username: name.val(), email: email.val() } ,function(m) {
someOtherFunction(m==1);
});
}
function someOtherFunction(parameter) {
// The parameter will be true or false
// depending on the value of m==1
}
Run Code Online (Sandbox Code Playgroud)
根据您的评论更新.
可能更好的只是把它$.get()带入submit(),但坚持你的原始想法,这是它的外观.
form.submit(function(){
// After usernameExists() is called, we need to hand off
// the rest of the execution to that function since
// this one will be done executing before the get()
// response is received
usernameExists();
return false;
});
function usernameExists() {
$.get("register.php",
{ check: 1, username: name.val(), email: email.val() },
// Have this callback take care of the rest of the submit()
function(m) {
if(m==1) {
// do something if true
} else {
// do something if false
}
}
);
}
Run Code Online (Sandbox Code Playgroud)
解释同步与异步javascript的乐趣
Javascript代码通常是同步执行的.这只意味着它一次执行一行,或者一行必须在下一行可以触发之前完成执行.
var greeting = "hi there"; // set the greeting variable
alert( greeting ); // the alert won't fire,
// until the previous line finished successfully
Run Code Online (Sandbox Code Playgroud)
这使事情变得非常好和可预测.但该规则有一些例外.一个值得注意的例外是AJAX调用.
你$.get()是一个AJAX调用的例子.AJAX中的"A"代表异步,这意味着它不会阻止下一行代码执行.
结果是,当你做一个$.get()需要(例如)1秒完成的时候,无论代码是在收到响应之后$.get()很久就完成了$.get().
以前面的greeting例子为例,但这次使用的是AJAX.
var greeting; // will hold the response from our AJAX call
$.get('some/path/to/data.php',
function( m ) {
greeting = m; // populate the greeting variable with the data returned
}
);
alert( greeting ); // Will alert "undefined" instead of the data that was returned
// because the $.get() code above is asynchronous, which allows
// the code below it (the alert in this case) to continue
// executing.
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,在收到响应alert( greeting )之前很久就会执行$.get(),因为它$.get()是异步的,并且在等待其数据时不会暂停执行链.
要解决此问题,您可以将回调置于alert() 内部$.get(),以便在收到响应之前它不会运行.
var greeting; // will hold the response from our AJAX call
$.get('some/path/to/data.php',
function( m ) {
greeting = m; // populate the greeting variable with the data returned
alert( greeting ); // Now the alert will give the expected result
// because it is in the callback.
}
);
Run Code Online (Sandbox Code Playgroud)
其结果是,在你的代码,一旦你打电话$.get(),依赖于接收到响应任何剩余的代码应该发生内回调.
将代码放在回调之外的唯一方法是将它放在自己的函数中,该函数从回调内部调用(就像我用我的原始答案做的那样).
代码运行方式的基本布局:
请记住,您不一定需要单独的功能usernameExists().您可以将所有代码放在submit()
form.submit(function() {
// Check to make sure input is valid **before** you send the AJAX
if(validateName() & validateEmail() & validatePass1() & validatePass2()) {
usernameExists(); // If valid, continue with the usernameExists()
}
return false; // We return false whether or not the content was valid,
// in order to prevent the form from submitting prematurely
});
function usernameExists() {
$.get("register.php",
{ check: 1, username: name.val(), email: email.val() },
// Have this callback take care of the rest of the submit()
function(m) {
// If "m" is less than one, there were no existing users
// so we can go ahead and post the data to the server
if( parseInt(m) < 1 ) {
// Here, you would need to manually do a post to
// submit the data to the server
$.post(url, data, callback, datatype );
}
}
);
}
Run Code Online (Sandbox Code Playgroud)
http://api.jquery.com/jquery.post/