Javascript HTML和JSF PrimeFaces-为什么actionListener不';当onClick存在时,是否不执行?

Javascript HTML和JSF PrimeFaces-为什么actionListener不';当onClick存在时,是否不执行?,javascript,html,jsf,primefaces,controller,Javascript,Html,Jsf,Primefaces,Controller,我正在使用HTML和JSF Primefaces,对于系统的这一部分,Primefaces没有启用(不知道为什么),问题是: 我有一个钮扣 <h:commandButton id="btnPrint" onclick = "switchDisabled();" actionListener="#{componentTrackingController.printReport()}" value="Print Report" /> 单击它的执行和工作良好(

我正在使用HTML和JSF Primefaces,对于系统的这一部分,Primefaces没有启用(不知道为什么),问题是:

我有一个钮扣

<h:commandButton id="btnPrint"
    onclick = "switchDisabled();"
    actionListener="#{componentTrackingController.printReport()}"
    value="Print Report"
/>

单击它的执行和工作良好(它只是禁用了一些其他按钮) 但actionListener不是。事实上,当我删除“onClick”行时,actionsListener工作正常

我的JavaScript代码很简单(但我认为问题不在这里)


函数switchDisabled(){
askDisabled(document.getElementById('mainForm:cboURdependences');
askDisabled(document.getElementById('mainForm:btnPrint');
askDisabled(document.getElementById('mainForm:btnCloseReport');
}
函数askDisabled(elem){
if(elem.disabled!=真){
elem.disabled=true;
}否则{
elem.disabled=false;
}
}  

我不熟悉jsf/PrimeFaces,但根据这个问题:

尝试使用“oncomplete”:


根据PrimeFaces文档(3.5):

oncomplete:ajax请求完成时要执行的Javascript处理程序


根据我的评论,它正在立即进行。 有几种方法可以解决这个问题(ajax挂钩)或在javascript中设置超时

<h:commandButton id="btnPrint"
    onclick = "setTimeout(function(){ switchDisabled() }, 1000);"
    actionListener="#{componentTrackingController.printReport()}"
    value="Print Report"
/>

这将绑定一个timout,以便在1秒(1000ms)内调用switchDisabled标记-因此您的ajax等应该已经运行并绑定了它,您会发现您可能会将其更改为500或更少,但您不希望它在调用submit之前触发它

参考:
有关详细信息

当您单击onclick时,将禁用按钮,它在提交之前被调用,因此它无法提交/单击禁用的按钮。您可以在ajaxListener的“onsuccess”中创建一个ajax钩子,然后调用switchDisabled()。它会在加载页面自动加载报告。您不能在h:commandButton上调用oncomplete-它需要嵌入ajax,需要使用p:commandButton来实现oncomplete。嗯,我假设p代表,部分,h:button的工作原理类似于aspx上的onclick/postback对,对吗?它是primefaces组件,它包装了标准的h:commandButton,默认情况下ajax提交,除非使用ajax=“false”,否则它会提交标准表单。它有自己的完整标签。谢谢你的解释非常感谢大家!!!onclick=“setTimeout(function(){switchDisabled()},1000);”
<h:commandButton id="btnPrint"
    oncomplete = "switchDisabled();"
    actionListener="#{componentTrackingController.printReport()}"
    value="Print Report"
/>
<h:commandButton id="btnPrint"
    onclick = "setTimeout(function(){ switchDisabled() }, 1000);"
    actionListener="#{componentTrackingController.printReport()}"
    value="Print Report"
/>