简体   繁体   English

使用f:ajax onevent时,布尔值不更新

[英]Boolean value not updating when using f:ajax onevent

When an option is selected it triggers a method call in the controller which changes the regionController.invalidRegion value (true to false and vice versa). 选择一个选项后,它将触发控制器中的方法调用,该方法将更改regionController.invalidRegion值(从true到false,反之亦然)。 The problem is after it changes the first time, the value is no longer update on the client side but changes correctly server side. 问题是第一次更改后,该值不再在客户端更新,而是在服务器端正确更改。

I have the following js; 我有以下js;

var renderRegionMsg = function(val){
    if(val === "true"){
        alert("true");
        document.getElementById("NavForm:regionform:notInRegion").style.visibility  = "";
    }
    else{
        alert("false");
        document.getElementById("NavForm:regionform:notInRegion").style.visibility  = "hidden";
    }
};

and the following jsf code: 和以下jsf代码:

<h:selectOneMenu id="regionSelect" >
    <f:selectItems ...... />
    <f:ajax 
      id="regionListener" 
      listener="#{geoListProducer.changeRegion}" 
      render="stripe-provState" 
      onevent="renderRegionMsg('#{regionController.invalidRegion}')" />     
</h:selectOneMenu>

<h:panelGroup id="notInRegion">
    <h:outputText class="red_bold"  value="#{userController.invalidRegionMsg}" />
</h:panelGroup>

You're not using <f:ajax onevent> the right way. 您没有以正确的方式使用<f:ajax onevent> The onevent attribute should refer a function reference, not contain an inline script like as you usually use on onclick and friends. onevent属性应引用函数引用,而不包含像通常在onclick和friends上使用的内联脚本。 The <f:ajax> will invoke the function reference three times, with an implicit data argument. <f:ajax>将使用隐式data参数调用函数引用三次。 One time before the ajax request is sent, one time after the ajax response is arrived, and one time after the HTML DOM is updated based on ajax response. 在发送ajax请求之前一次,在到达ajax响应之后一次,以及在基于ajax响应更新HTML DOM之后一次。

Here's a kickoff example of the correct usage: 这是正确用法的启动示例:

function functionName(data) {
    var status = data.status; // Can be "begin", "complete" or "success".
    var source = data.source; // The parent HTML DOM element.

    switch (status) {
        case "begin": // Before the ajax request is sent.
            // ...
            break;

        case "complete": // After the ajax response is arrived.
            // ...
            break;

        case "success": // After update of HTML DOM based on ajax response.
            // ...
            break;
    }
}

Which is to be declared as follows: 声明如下:

<f:ajax ... onevent="functionName" />

In your particular case , you likely need the following approach: 您的特定情况下 ,您可能需要以下方法:

function renderRegionMsg(data) {
    if (data.status == "success") {
        var val = data.source.value;

        if (val === "true") {
            alert("true");
            document.getElementById("NavForm:regionform:notInRegion").style.visibility  = "";
        }
        else {
            alert("false");
            document.getElementById("NavForm:regionform:notInRegion").style.visibility  = "hidden";
        }
    }
}

with

<f:ajax ... onevent="renderRegionMsg" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM