我有这个代码:
<div id="visibleButtons">
<p:commandButton id="hint" action="#{tutorSession.hintRequest}" value="Hint Please" update="@parent" oncomplete="playNextHint(#{tutorSession.giveWarning});">
</p:commandButton>
<p:commandButton id="newType" action="#{tutorSession.newPathRequest}" value="Something Else" update="@parent" oncomplete="playNextHint(#{tutorSession.giveWarning});">
</p:commandButton>
#{tutorSession.giveWarning}<!-- I will remove this line once the buttons are working -->
</div>
所发生的情况是,javascript 函数 playNextHint 正在使用上次调用的 GiveWarning 值执行,而不是当前调用的值。我检查服务器是否返回正确的值;这是。事实上,我在底部添加了一点文本输出来验证是否返回了正确的值,并且它始终按照我期望的方式显示,但 javascript 仍然使用旧值。
对可能发生的事情有什么想法吗?
JavaScript 方法(如果相关):
function playNextHint(giveWarning){
alert(giveWarning);
}
提前谢谢您。
编辑:我找到了一种解决方法,方法是使用带有值的隐藏字段,然后在 javascript 中获取该字段的值,但这很困惑,必须有更好的方法,正如我将要做的那样这个网络应用程序有很多这样的事情。
编辑2:抱歉,我出于演示目的留下了一些代码,这些代码似乎让人们感到困惑。首先,我不想显示 GiveWarning。就在那里,所以我可以看到它正在获得正确的值。我将其保留下来,以便人们可以看到正确的值出现在哪里,而不是没有出现在哪里。
编辑3:按下按钮时我想要做的是计算服务器上的giveWarning的新值(该部分有效),然后将其发送到javascript函数以基于它执行逻辑(警报是占位符并用于测试)。问题是,javascript 函数获取的值似乎总是比应有的值落后一键,即使当我将值发送到屏幕(div 中的最后一行)时,它会显示应有的内容。对所有的编辑表示抱歉;正如我所说,这是我第一次发帖。
最佳答案
我认为您只是采取了不适当的方法。我刚刚做了一个新的<outputLabel>
并给它 id
在 javascript
中使用它方法并添加update="label"
更新标签并消除向此 js
传递任何参数方法。你可以检查这个
在 .xhtml
<h:form id="myForm">
<p:commandButton id="hint" action="#{tutorSession.hintRequest}" value="Hint Please" update="label" oncomplete="playNextHint();">
</p:commandButton>
<p:commandButton id="newType" action="#{tutorSession.newPathRequest}" value="Something Else" update="label" oncomplete="playNextHint();">
</p:commandButton>
<p:commandButton id="skip" action="#{tutorSession.giveUp}" value="Skip">
</p:commandButton>
<h:outputLabel value="#{tutorSession.giveWarning}" id="label" />
</h:form>
还有你的js
功能
function playNextHint(){
alert($('#myForm\\:label').text());
}
并在您的 ManagedBean 中 tutorSession
// other stuff here
private String giveWarning;
// setters / getters for giveWarning
public void hintRequest() {
setGiveWarning("hint");
}
public void newPathRequest() {
setGiveWarning("newPath");
}
关于javascript - oncomplete 方法中的 PrimeFaces 属性未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28569289/