简体   繁体   中英

Checkbox's onchange method not working with jsf and javascript

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!

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).

You'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}) ..."

I do not know JSF but IMHO it should be

if(this.checked && this.value=='bundleAdded'){alert('Worked');}else{alert('failed');}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM