问题
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?
回答1:
Firstly you need to add the return keyword to your onclick handler... try changing onclick="show_confirm()" to onclick="return show_confirm()"
secondly, you can simplify your function to:
function show_confirm()
{
return confirm("Are you sure you want to do this? This is totally hazardous!");
}
回答2:
For JSF ajax you can do with following way.
<script>
function show_confirm()
{
return confirm("Are you sure you want to do this? This is totally hazardous!");
}
</script>
<h:commandButton value="A Hazardous Button!" onclick="return show_confirm()">
<f:ajax listener=#{bean.doSomething}"/>
</h:commandButton>
来源:https://stackoverflow.com/questions/6402222/aborting-an-ajax-request-if-confirmation-dialog-is-cancel