【问题标题】:Javascript and <p:ajax> behave strangeJavascript 和 <p:ajax> 行为奇怪
【发布时间】:2013-02-13 16:11:39
【问题描述】:

我有一个 js 函数 codeAddress() 处理来自 address 的数据并更新 fullAddressvalidField 的值。
fullAddressvalidField 的数据由 &lt;p:ajax&gt; 传递给支持 bean,但 setter 方法似乎被调用一个请求延迟。
alert() 末尾的 codeAddress 显示正确的新数据。
address 具有onchange="test()" 时,一切正常,支持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);
    });
};

【问题讨论】:

  • @geocodezip 谢谢你的提示。这个问题听起来确实很像我的。但是我没有使用回调函数的结果吗?警报始终显示正确的值。我想念什么?我是否需要某种延迟或积极等待响应?
  • “我没有使用回调函数的结果”是什么意思?那是它唯一可用的地方。
  • @geocodezip 很抱歉表达了误解。这句话的意思应该是“我正在使用回调函数的结果。当你看我的代码时,你不也是这么想的吗?”你有一个想法,如何让它发挥作用?我真的很感激。 :)
  • @geocodezip 我现在建立了一个静态延迟,它似乎可以工作。有没有办法避免静态延迟并“抓住”地理编码器的答案?

标签: javascript jsf-2 google-maps-api-3 primefaces


【解决方案1】:

这不是重复的问题。
第一个错误是使用&lt;p:ajax&gt; 提交将由JS 更改的数据,因为在脚本仍在运行时已经准备好ajax 请求。 第二个错误是使用非跨浏览器兼容的jsf.ajax.request。 现在我只是删除了&lt;p:ajax&gt; 并使用jQuery 解决方案。这个函数只是从地理编码器回调函数中调用的:

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;
        }
    }});
};

【讨论】:

    猜你喜欢
    • 2013-01-20
    • 1970-01-01
    • 2011-07-28
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多