开发者

JSF: JS redirection doesn't work for h:commandButton onclick event

开发者 https://www.devze.com 2023-04-01 04:55 出处:网络
Migrating from a4j:commandLink to h:commandButton problem. this working case: <a4j:commandLinkevent=\"onclick\" onclick=\"if (!confirm(\'#{resourceBundle.agreement_cancel_message}\')) return fals

Migrating from a4j:commandLink to h:commandButton problem.

this working case:

<a4j:commandLink  event="onclick" onclick="if (!confirm('#{resourceBundle.agreement_cancel_message}')) return false;" oncomplete="window.location.href='menu?agreementId='+ agreementId;"/>

this case with h:commandButton doesn't work,current page just refreshed:

开发者_JS百科
<h:commandButton onclick="if (!confirm('#{resourceBundle.agreement_cancel_message}')) return false;window.location.href='menu?agreementId='+ agreementId;"/>

As I know,I can't use oncomplete for h:commandButton at all.


You need to always return false in order to prevent the button's default action (submitting the form).

<h:commandButton onclick="if(confirm('#{resourceBundle.agreement_cancel_message}'))window.location.href='menu?agreementId='+ agreementId;return false;"/>

(putting {} around the if body may improve readability)

By the way, you seem to never invoke a bean action method. Why are you using <h:commandButton> at all?

<button onclick="if(confirm('#{resourceBundle.agreement_cancel_message}'))window.location.href='menu?agreementId='+ agreementId;return false;"/>


Page is refreshed because default event handler of input is working.

Try preventing input's submit behavior (return false; at the end):

<h:commandButton onclick="if (!confirm('#{resourceBundle.agreement_cancel_message}')) return false;window.location.href='menu?agreementId='+ agreementId; return false;"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消