I’d like to have a <f:ajax> component that would display a confirmation dialog and then do the action tag if OK was selected or abort if Cancel was selected.
I thought this would work, but it doesn’t (The ajax part runs no matter what I select in the confirmation):
<f:ajax render="some_div">
<h:commandButton value="A Hazardous Button!"
onclick="show_confirm()" action="#{bean.doSomething}" />
</f:ajax>
–
function show_confirm()
{
var r=confirm("Are you sure you want to do this? This is totally hazardous!");
if (r==true)
{
return true;
}
else
{
return false;
}
}
Any idea why?
Firstly you need to add the
returnkeyword to your onclick handler… try changingonclick="show_confirm()"toonclick="return show_confirm()"secondly, you can simplify your function to: