javascript 单击后禁用 `<h:commandButton>`,但应触发操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13116890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:50:43  来源:igfitidea点击:

Disable `<h:commandButton>` after clicked, but action should be fired

javascriptjsfajax4jsf

提问by prageeth

I have a <h:commandButton>in my XHTML page. I want the button to be disabled as soon as I click the button. Then the button should call the action. My button gets disabled as I expect but the problem is that the action is not fired.
<h:commandButton value="Go" onclick="this.disabled=true" action="#{bean.go()}"/>

<h:commandButton>我的 XHTML 页面中有一个。我希望在单击按钮后立即禁用该按钮。然后按钮应该调用操作。我的按钮按我的预期被禁用,但问题是未触发该操作。
<h:commandButton value="Go" onclick="this.disabled=true" action="#{bean.go()}"/>

But if I remove the onclickattribute the action is fired.

但是,如果我删除该onclick属性,则会触发该操作。

Or if I used an <a4j:commandButton>it works.
Following a4j button works.
<a4j:commandButton value="Go" onclick="this.disabled=true" action="#{bean.go()}"/>

或者,如果我使用<a4j:commandButton>它,它会起作用。
以下 a4j 按钮有效。
<a4j:commandButton value="Go" onclick="this.disabled=true" action="#{bean.go()}"/>

How can I disable a <h:commandButton>after being clicked, so that the action still fires?

如何<h:commandButton>在单击后禁用 a ,以便操作仍然触发?

采纳答案by Daniel

Use setTimeout, that way the action will be fired and the button will be disabled...

使用setTimeout,这样操作将被触发,按钮将被禁用......

<h:commandButton value="Go"
   onclick="setTimeout('this.disabled = true;', 50);" 
   action="#{bean.go()}"/>

You can also take a look at the following question too :How can I disable an h:commandButton without preventing the action and actionListener from being called?

您还可以查看以下问题:如何在不阻止调用 action 和 actionListener 的情况下禁用 h:commandButton?