Javascript和<;p:ajax>;举止古怪

Javascript和<;p:ajax>;举止古怪,javascript,jsf-2,google-maps-api-3,primefaces,Javascript,Jsf 2,Google Maps Api 3,Primefaces,我有一个js函数codeAddress(),它处理address中的数据,并更新fullAddress和validField fullAddress和validField的数据通过传递给支持bean,但是setter方法似乎被调用一个请求延迟。 codemaddress末尾的alert()显示正确的新数据。 当address具有onchange=“test()”一切正常时,将立即调用backing bean setter方法。 我不知道这里可能出了什么问题 我的jsf代码: <html x

我有一个js函数
codeAddress()
,它处理
address
中的数据,并更新
fullAddress
validField

fullAddress
validField
的数据通过
传递给支持bean,但是setter方法似乎被调用一个请求延迟。
codemaddress
末尾的
alert()
显示正确的新数据。
address
具有
onchange=“test()”
一切正常时,将立即调用backing bean setter方法。
我不知道这里可能出了什么问题

我的jsf代码:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
<h:head>
        <script src="https://maps.googleapis.com/maps/api/js?v=3&amp;sensor=false"></script>
</h:head>

<h:body>
    <h:outputScript name="jsf.js" library="javax.faces" target="head"/>
    <h:outputScript library="js" name="bugMaps.js" />
    <body onload="initialize()" />
    <h:form id="addressForm">
        <p:inputText id="address" onchange="codeAddress()">
            <p:ajax process="fullAddress validField"/>
        </p:inputText>
        <p:message id="addressValidate" for=":addressForm:validField"/>
        <p:commandButton value="submit" />
        <p:inputText id="fullAddress" value="#{addressBean.fullAddress}" />
        <p:inputText id="validField" value="#{addressBean.valid}" />
    </h:form>

</h:body>
</html>

我的JS:

var geocoder;
var map;
var valid = false;
var fullAddress = "none";

function initialize() {
    geocoder = new google.maps.Geocoder();
}

function test(){
    fullAddress = Math.random();
    document.getElementById('addressForm:fullAddress').value = fullAddress;
}

function codeAddress() {
    var address = (document.getElementById('addressForm:address').value + ", Germany");
    geocoder.geocode({'address' : address},function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var latLong = results[0].geometry.location;
            var latitude = results[0].geometry.location.lat();
            var longitude = results[0].geometry.location.lng();
            var country, postal_code, locality, street_number, route;
            for (i = 0; i < results[0].address_components.length; ++i) {
                var component = results[0].address_components[i];
                if (!locality && component.types.indexOf("locality") > -1)
                    locality = component.long_name;
                else if (!postal_code && component.types.indexOf("postal_code") > -1)
                    postal_code = component.long_name;
                else if (!country && component.types.indexOf("country") > -1)
                    country = component.long_name;
                else if (!street_number && component.types.indexOf("street_number") > -1)
                    street_number = component.long_name;
                else if (!route && component.types.indexOf("route") > -1)
                    route = component.long_name;
            }
            if (typeof latLong != "undefined"
                && typeof latitude != "undefined"
                && typeof longitude != "undefined"
                && typeof route != "undefined"
                && typeof street_number != "undefined"
                && typeof postal_code != "undefined"
                && typeof locality != "undefined"
                && typeof country != "undefined"){
                valid = true;
                fullAddress = results[0].formatted_address;
            }
            else{
                valid=false;
                fullAddress="none";
            };
        }
        else{

            valid=false;
            fullAddress="none";
        }
        document.getElementById('addressForm:fullAddress').value = fullAddress;
        document.getElementById('addressForm:validField').value = valid;
        alert(fullAddress + valid);
    });
};
var地理编码器;
var映射;
var-valid=false;
var fullAddress=“无”;
函数初始化(){
geocoder=新的google.maps.geocoder();
}
功能测试(){
fullAddress=Math.random();
document.getElementById('addressForm:fullAddress')。value=fullAddress;
}
函数代码地址(){
var address=(document.getElementById('addressForm:address').value+“,德国”);
geocoder.geocode({'address':address},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
var latLong=results[0]。geometry.location;
var latitude=results[0]。geometry.location.lat();
var longitude=results[0]。geometry.location.lng();
var国家、邮政编码、地点、街道号、路线;
对于(i=0;i-1)
Location=component.long\u名称;
else if(!postal_code&&component.types.indexOf(“postal_code”)>-1)
邮政编码=组件.long\u名称;
else if(!country&&component.types.indexOf(“country”)>-1)
国家/地区=组件.long\u名称;
如果(!street\u number&&component.types.indexOf(“street\u number”)>-1,则为else
街道编号=组件长名称;
else if(!route&&component.types.indexOf(“route”)>-1)
路由=组件.long\u名称;
}
如果(latLong的类型!=“未定义”
&&纬度类型!=“未定义”
&&经度类型!=“未定义”
&&路由类型!=“未定义”
&&街道类型编号!=“未定义”
&&邮政编码类型!=“未定义”
&&地区类型!=“未定义”
&&国家类型!=“未定义”){
有效=真;
fullAddress=结果[0]。格式化的\u地址;
}
否则{
有效=错误;
fullAddress=“无”;
};
}
否则{
有效=错误;
fullAddress=“无”;
}
document.getElementById('addressForm:fullAddress')。value=fullAddress;
document.getElementById('addressForm:validField')。值=有效;
警报(完整地址+有效);
});
};

这不是一个重复的问题。
第一个错误是使用
提交要由JS修改的数据,因为ajax请求已经准备好,而脚本仍在运行。 第二个错误是使用不兼容跨浏览器的
jsf.ajax.request
。 现在我只是删除了
,并使用jQuery的解决方案。此函数仅从geocoders回调函数调用:

function jsfSubmit(){
    document.getElementById('addressForm:fullAddress').value = fullAddress;
    document.getElementById('addressForm:validField').value = valid;
    $.ajax({type:'POST', data:$('#addressForm').serialize(), success: function(response) {
        if(valid){
            var destPage = 'nextPage.xhtml';
            window.location = destPage;
        }
    }});
};

@geocodezip可能重复谢谢您的提示。这个问题听起来确实很像我的问题。但是我不是在使用回调函数的结果吗?警报始终显示正确的值。我错过了什么?我需要某种延迟还是主动等待响应?你说的“我没有使用回调函数的结果”是什么意思?这是它唯一可用的地方。@geocodezip很抱歉表示误解。这句话的意思应该是“我正在使用回调函数的结果。当你看到我的代码时,你不也这么认为吗?”你有什么想法,如何让它工作?我将非常感激。:)@geocodezip我现在构建了一个静态延迟,它似乎可以工作。有没有一种方法可以避免静态延迟,并在出现时“捕获”地理编码器的答案?