问题
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