Javascript Facebook注册插件+;验证无法从函数返回值
我在Facebook注册插件中使用了一个自定义字段,需要验证一个返回布尔值的zipcode字段,不知何故,这是因为这个内部函数:Javascript Facebook注册插件+;验证无法从函数返回值,javascript,facebook,Javascript,Facebook,我在Facebook注册插件中使用了一个自定义字段,需要验证一个返回布尔值的zipcode字段,不知何故,这是因为这个内部函数: geocoder.geocode({ address: address }, function (results, status) { } 我不能向addExist变量返回值,它总是返回“未定义”。有人能帮忙吗?谢谢 <fb:registration redirect_uri="http://localhost:40153/Account/Regist
geocoder.geocode({ address: address }, function (results, status) {
}
我不能向addExist变量返回值,它总是返回“未定义”。有人能帮忙吗?谢谢
<fb:registration redirect_uri="http://localhost:40153/Account/RegisterDestination.aspx"
fields='[
{"name":"name"},
{"name":"gender"},
{"name":"email"},
{"name":"birthday"},
{"name":"password"},
{"name":"zip","description":"Postal Code","type":"text"}]'
onvalidate="validate"></fb:registration>
function validate(form) {
errors = {};
if (form.zip !== "") {
var addExist = calculateCoordinates(form.zip.toString());
// alert(addExist);
if (!addExist) {
errors.zip = "Cannot locate address";
}
}
return errors;
}
function calculateCoordinates(zip) {
var txtPostcode = zip;
// var address = txtAddress1.value + ', ';
// address += txtAddress2.value + ', ';
// address += txtTown.value + ', ';
var address = txtPostcode;
// address += txtCountry.value;
var geocoder;
geocoder = new google.maps.Geocoder();
geocoder.geocode({ address: address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var location = results[0].geometry.location;
var litLatClientId = document.getElementById("<%=litLat %>");
var litLongClientId = document.getElementById("<%=litLong %>");
$("#" + litLatClientId).val(location.lat());
$("#" + litLongClientId).val(location.lng());
// Successfully reach here but the bool variable 'addExist' value gets an 'Undefined' value.
return true;
}
else
return false;
});
}
函数验证(表单){
错误={};
如果(form.zip!==“”){
var addExist=计算坐标(form.zip.toString());
//警报(addExist);
if(!addExist){
errors.zip=“无法找到地址”;
}
}
返回错误;
}
函数计算坐标(zip){
var txtPostcode=zip;
//变量地址=txtAddress1.value+',';
//地址+=txtAddress2.value+',';
//地址+=txtTown.value+',';
var地址=TXT邮政编码;
//地址+=txtCountry.value;
var地理编码器;
geocoder=新的google.maps.geocoder();
geocoder.geocode({address:address},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
var location=results[0]。geometry.location;
var litLatClientId=document.getElementById(“”);
var litLongClientId=document.getElementById(“”);
$(“#”+litLatClientId.val(location.lat());
$(“#”+litLongClientId.val(location.lng());
//已成功到达此处,但布尔变量“addExist”值获取“Undefined”值。
返回true;
}
其他的
返回false;
});
}
计算坐标
正在使用地理编码
,这是一个异步函数。因此,它本身必须异步使用,以便从内部异步函数“返回”任何数据。这是通过使用回调模式完成的
不幸的是,您正在同步使用它,注册插件也希望validate
同步使用。因此,根本没有办法使这项工作成功