最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Checkbox's onchange method not working with jsf and javascript - Stack Overflow

programmeradmin8浏览0评论

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 badges
Add a ment  | 

3 Answers 3

Reset to default 3

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)

  1. 暂无评论