My codes are bellow,
JSF :
<h:selectBooleanCheckbox id="bundleAdded" value="#{accountAdjustmentBean.bundleAdded}"
required="true" onchange="if(this.checked != bundleAdded) {
alert('Click works')}
else { alert('Not worked!') }"/>
<h:message styleClass="errors" for="bundleAdded"/>
My backing bean :
public class AccountAdjustmentsBean extends BaseBean {
private boolean bundleAdded;
// public setters, getters and other stuffs
}
face-config.xml :
<managed-bean>
<description>
Backing bean used do account adjustments
</description>
<managed-bean-name>accountAdjustmentBean</managed-bean-name>
<managed-bean-class>beyondm.bi.customercare.view.bean.AccountAdjustmentsBean</managed-bean-class>
<!-- NOTE!: proper behaviour of this bean relies on being created for each request -->
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>serviceLocator</property-name>
// propagates.......
When I checked the box, there is no alert. Where is the problem? I can't figure out it. Can anyone point out it?
Thanks!
My codes are bellow,
JSF :
<h:selectBooleanCheckbox id="bundleAdded" value="#{accountAdjustmentBean.bundleAdded}"
required="true" onchange="if(this.checked != bundleAdded) {
alert('Click works')}
else { alert('Not worked!') }"/>
<h:message styleClass="errors" for="bundleAdded"/>
My backing bean :
public class AccountAdjustmentsBean extends BaseBean {
private boolean bundleAdded;
// public setters, getters and other stuffs
}
face-config.xml :
<managed-bean>
<description>
Backing bean used do account adjustments
</description>
<managed-bean-name>accountAdjustmentBean</managed-bean-name>
<managed-bean-class>beyondm.bi.customercare.view.bean.AccountAdjustmentsBean</managed-bean-class>
<!-- NOTE!: proper behaviour of this bean relies on being created for each request -->
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>serviceLocator</property-name>
// propagates.......
When I checked the box, there is no alert. Where is the problem? I can't figure out it. Can anyone point out it?
Thanks!
Share Improve this question edited Aug 10, 2011 at 6:06 Abimaran Kugathasan asked Aug 10, 2011 at 5:24 Abimaran KugathasanAbimaran Kugathasan 32.5k11 gold badges80 silver badges108 bronze badges3 Answers
Reset to default 3You're expecting the bundleAdded
property to be magically present as a JavaScript variable in the onclick
function scope. This is not true. Java/JSF and JavaScript does not run in the same environment. You should see Java/JSF more as a HTML/CSS/JS code generator. Java/JSF runs in webserver, generates HTML/CSS/JS and sends it to webbrowser. HTML/CSS/JS runs in the webbrowser.
You need to let Java/JSF print the bundleAdded
property value using EL.
onclick="if(this.checked != #{accountAdjustmentBean.bundleAdded}) ..."
This condition is invalid: if(this.checked != bundleAdded) I think you just need to do: if(this.checked)
If you are trying to see if the value (bundleAdded) has actually been set then you shouldn't use Javascript to do it as this is only on the client side (unless of course you use Ajax but looking at your example I can't see why you would want to).
I do not know JSF but IMHO it should be
if(this.checked && this.value=='bundleAdded'){alert('Worked');}else{alert('failed');}