How to programmatically trigger onclick event

大兔子大兔子 提交于 2019-12-24 17:04:41

问题


How can I programmatically trigger onclick/oncompleted event? E.g.

<p:commandButton value="Destroy the World" onclick="confirmation.show()" type="button"/>  

<p:confirmDialog message="Are you sure about destroying the world?"  
                 showEffect="bounce" hideEffect="explode"  
                 header="Initiating destroy process" severity="alert" widgetVar="confirmation">
</p:confirmDialog>

I'm trying to trigger onclick="confirmation.show()" from a backing bean. I'm using JSF2/Primefaces 2.2.


回答1:


You should use the oncomplete attribute instead of onclick of the commandButton. The click javascript event occurs before the page posts back, likely causing your dialog not to appear because of the page reloading.

oncomplete="confirmation.show()" will display the dialog AFTER the postback.




回答2:


Since the click contains only one call, you can call the code directly:

confirmation.show();

But if you want to do it your way and if you use jQuery, you can simply do:

$('p[type=button]').click();


来源:https://stackoverflow.com/questions/7281821/how-to-programmatically-trigger-onclick-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!