Aborting an Ajax request if confirmation dialog is "Cancel"
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 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!");
}
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>
精彩评论