[英]Aborting an Ajax request if confirmation dialog is “Cancel”
我想要一個<f:ajax>
組件,它會顯示一個確認對話框,然后在選擇OK
時執行action
標記,如果選擇了Cancel
則中止。 我認為這會起作用,但事實並非如此(無論我在確認中選擇什么,ajax部分都會運行):
<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;
}
}
知道為什么嗎?
首先,您需要將return
關鍵字添加到您的onclick處理程序中...嘗試將onclick="show_confirm()"
更改為onclick="return show_confirm()"
其次,您可以簡化您的功能:
function show_confirm()
{
return confirm("Are you sure you want to do this? This is totally hazardous!");
}
對於JSF ajax,您可以使用以下方式。
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.