简体   繁体   English

JSF在onClick事件之前执行Ajax

[英]JSF execute Ajax before a onClick Event

I have a Javascript function which changes the position of some elements according to some hiddenfields in the document, which are updated regularly with ajax. 我有一个Javascript函数,可根据文档中的某些隐藏字段更改某些元素的位置,这些字段会使用ajax定期更新。

Problem: 问题:

Since the javascript function (onclick) is executed before the Ajax (which reloads the element in which the hiddenfields are) the elements which should change position are always one turn behind. 由于javascript函数(onclick)是在Ajax(重新加载hiddenfield所在的元素)之前执行的,因此应更改位置的元素总是落后一圈。

What I want to achive: 我要达到的目标:

The Problem would be solved if the ajax reload is executed before the onclick Event, so the reference to the hiddenfields would be correct. 如果在onclick事件之前执行了ajax重新加载,则将解决该问题,因此对hiddenfields的引用将是正确的。 (and not on turn behind) (而不是在后面)

Is this possible in any way or is there another solution to this problem?? 是否有可能以任何方式解决此问题?

Code: 码:

Call: 呼叫:

                <h:commandButton id="dice" onclick="animate()" alt="W&uuml;rfel mit einer Eins" image="resources/img/wuerfel1.png" action="#{spiel.dice()}" tabindex="4" title="W&uuml;rfel mit einer Eins">
                    <f:ajax render="gameinfo" />                            
                </h:commandButton>

Javascript Function: JavaScript函数:

    function animate() {
        var newPlayer1 = document.getElementById('player1score').value;
        var newPlayer2 = document.getElementById('player2score').value;
        // Spieler 1 Animation
        $("#player1").fadeOut(700, function() {
            $("#player1").appendTo(newPlayer1);
            $("#player1").fadeIn(700);
});
        // Spieler2 Animation
        $("#player2").delay(1400).fadeOut(700, function() {
            $("#player2").appendTo(newPlayer2);
            $("#player2").fadeIn(700);
});
    }

You should use the onevent of the f:ajax 您应该使用f:ajaxonevent

  <h:commandButton id="dice" alt="W&uuml;rfel mit einer Eins" image="resources/img/wuerfel1.png" action="#{spiel.dice()}" tabindex="4" title="W&uuml;rfel mit einer Eins">
      <f:ajax render="gameinfo" onevent="animate" />                            
  </h:commandButton>

change your animate function into 将您的animate功能更改为

function animate(data) {
    if (data.status === 'success') {
        //your original code goes here...
    }
}

Also : read this answer by BalusC : Proccess onclick function after ajax call 另外:通过BalusC阅读此答案: ajax调用后,过程onclick函数

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

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