【问题标题】:How to call a javascript function and an ajax command by pressing one button?如何一键调用javascript函数和ajax命令?
【发布时间】:2013-05-06 00:32:12
【问题描述】:

所以我想要的是在表单中使用 1 个按钮(因为 ajax 需要在表单中?) 到

1) 调用一个javascript函数,它基本上只是做一些动画(如淡出和淡入)

2) 调用更新“信息”表的 ajax 函数

所以基本上我想要的只是单击一个按钮,例如“开始” 然后 javascript 做了一些客户端的事情,比如在对象周围移动,ajax 给我关于存储在服务器上的那些对象的信息。..

我们的东西是这样的:

<h:panelGroup layout="block" id="player" class="player">
                    <h2 class="accessibility">W&uuml;rfelbereich</h2>
                    <span class="accessibility">An der Reihe ist</span><div id="currentPlayerName"><h:outputText value=" #{spiel.getSpieler_Name()}" /></div>
                    <h:commandButton id="dice" alt="W&uuml;rfel" image="resources/img/wuerfel0.png" action="#{spiel.dice()}" tabindex="4" title="W&uuml;rfel">
                        <f:ajax render=":gameinfo" onevent="animate" />
                    </h:commandButton>
                </h:panelGroup>

因此 gameinfo 是从服务器检索并使用 ajax 调用的信息表

并且 animate 是我们的 javascript 函数,它执行动画..

这里的问题是,一旦我们将 ajaax 命令放入表单标签中,animate 就不起作用了

【问题讨论】:

  • animate 函数是立即返回还是等到 10 秒动画完成?

标签: javascript html ajax jsf xhtml


【解决方案1】:

试试这个:

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

【讨论】:

  • 不起作用,因为 onclick 会以某种方式禁用 ajax 功能 =(
  • 如果animate 函数返回false,它会,否则它应该工作。
  • 或者可能还有另一种情况——如果animate函数需要一些时间(即动画结束后返回)那么ajax不会立即发送。
猜你喜欢
  • 2015-07-11
  • 2013-01-26
  • 1970-01-01
  • 2013-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多