jQuery检查返回未定义 [英] jQuery check returns undefined
问题描述
我在jQuery方面的经验是有限的,我搜索了一个答案,但似乎无法找到它。
该函数返回false或true上得到。现在我的问题是,如果我得到我的数据,函数可用性不会返回false和true。
这是代码。
function Availability(){
var email = jQuery('#emailRegister')。val();
jQuery.get(test.php,{email:email})
.done(function(data){
if(data =='true'){
jQuery(。warningEmailDuplicate)。hide();
return true;
} else if(data =='false'){
jQuery(。warningEmailDuplicate)。show() ;
返回false;
}
});
jQuery.get
创建一个AJAX请求。这些是异步的。您不能返回
AJAX回调函数中的值。
您需要重写您的代码才能使用回调:
函数可用性(){
var email = jQuery('#emailRegister')。val();
jQuery.get(test.php,{email:email})
.done(function(data){
if(data =='true'){
jQuery(。warningEmailDuplicate)。hide();
AvailabilityResult(true);
} else if(data =='false'){
jQuery(。warningEmailDuplicate)。show ();
AvailabilityResult(false);
}
});
}
函数AvailabilityResult(可用){
// *可用*将为true或false
//用这个值做你想做的事返回值来自可用性
}
My experience in jQuery is limited and I've searched around for an answer but can't seem to find it.
This function returns a false or true based on a get. Now my problem is that the function 'Availability' doesn't return a false nor true if I get my data.
This is the code.
function Availability(){
var email = jQuery('#emailRegister').val();
jQuery.get("test.php", {email: email})
.done(function(data) {
if(data == 'true'){
jQuery(".warningEmailDuplicate").hide();
return true;
}else if(data == 'false'){
jQuery(".warningEmailDuplicate").show();
return false;
}
});
}
jQuery.get
creates an AJAX request. These are asynchronous. You cannot return
a value from the AJAX callback to the calling function. The function will have exited, returning nothing, before the HTTP request completes.
You need to rewrite your code to use callbacks:
function Availability(){
var email = jQuery('#emailRegister').val();
jQuery.get("test.php", {email: email})
.done(function(data) {
if(data == 'true'){
jQuery(".warningEmailDuplicate").hide();
AvailabilityResult(true);
}else if(data == 'false'){
jQuery(".warningEmailDuplicate").show();
AvailabilityResult(false);
}
});
}
function AvailabilityResult(available) {
// *available* will be true or false
// do with this value what you wanted to do with the return value from Availability
}
这篇关于jQuery检查返回未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!