开发者

Checkbox's onchange method not working with jsf and javascript

开发者 https://www.devze.com 2023-03-27 03:14 出处:网络
My codes are bellow, JSF : <h:selectBooleanCheckbox id=\"bundleAdded\" value=\"#{accountAdjustmentBean.bundleAdded}\"

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&g开发者_如何学Got;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!


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


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');}
0

精彩评论

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