【问题标题】:HTML and JSF PrimeFaces - Why actionListener doesn't execute when onClick is present?HTML 和 JSF PrimeFaces - 为什么存在 onClick 时 actionListener 不执行?
【发布时间】:2014-04-26 11:39:59
【问题描述】:

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

我有一个按钮

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

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

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

<script type="text/javascript">

    function switchDisabled(){
        askDisabled(document.getElementById('mainForm:cboURdependences'));
        askDisabled(document.getElementById('mainForm:btnPrint'));
        askDisabled(document.getElementById('mainForm:btnCloseReport'));
    }

    function askDisabled(elem){
        if (elem.disabled != true){
            elem.disabled = true;
        }else{
            elem.disabled = false;
        }
    }  

</script>    

【问题讨论】:

  • 您在执行 onclick 时禁用了按钮,它在提交之前被调用,因此它无法提交/单击禁用的按钮。您可以为您的 ajaxListener 创建一个 ajax 挂钩到“onsuccess”,然后调用 switchDisabled()
  • 它会在 loadingPage 自动加载报告

标签: javascript html jsf primefaces controller


【解决方案1】:

我不熟悉 jsf/PrimeFaces,但根据这个问题:Primefaces onclick and onsuccess differences

尝试使用“oncomplete”:

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

来自 PrimeFaces 文档 (3.5):

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

【讨论】:

  • 您不能在 h:commandButton 上调用 oncomplete - 它需要嵌入 ajax,需要使用 p:commandbutton 来实现 oncomplete。
  • 嗯,我假设 p 是 for,partial,h:button 的工作方式类似于 aspx 上的 onclick/postback 对,我说得对吗?
  • 它是primefaces组件,它封装了标准的h:commandButton,并且默认做ajax提交,除非你使用ajax="false"然后它做标准的表单提交。它有自己的完整标签。
  • 感谢您的解释
【解决方案2】:

根据我的评论,它会立即执行。 有几种方法可以解决这个问题,(ajax 钩子)或 javascript 中的 setTimeout。

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

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

参考:http://www.w3schools.com/jsref/met_win_settimeout.asp 了解更多信息

【讨论】:

  • 非常感谢你们!!! onclick = "setTimeout(function(){ switchDisabled() }, 1000);"
猜你喜欢
  • 1970-01-01
  • 2012-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-11
  • 2016-09-17
  • 1970-01-01
相关资源
最近更新 更多