2011-06-19 32 views
1

我想有一個<f:ajax>組件如果選擇OK,將顯示一個確認對話框,然後執行action標籤,或者選擇Cancel中止。 我想這會工作,但它不會(AJAX的一部分,無論我在確認選擇運行):中止,如果確認對話框是一個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; 
    } 
} 

任何想法,爲什麼?

回答

3

首先,你需要將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!"); 
} 
+0

Thanx!這工作... – Ben

1

對於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>