简体   繁体   English

复选框的 onchange 方法不适用于 jsf 和 javascript

[英]Checkbox's onchange method not working with jsf and javascript

My codes are bellow,我的代码如下,

JSF: 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:面配置.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:= bundleAdded) 我认为您只需要这样做。 if(this.checked)如果(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).如果您正在尝试查看值 (bundleAdded) 是否已实际设置,那么您不应使用 Javascript 来执行此操作,因为这仅在客户端(当然,除非您使用 Ajax 但看看您的示例我不能看看你为什么想要)。

You're expecting the bundleAdded property to be magically present as a JavaScript variable in the onclick function scope.您期望bundleAdded属性神奇地作为 JavaScript 变量出现在onclick ZC1C425268E68385D1AB5074C17A94F141Z8251A1FD114022E8ADF9A5B6D34FD45EZ This is not true.这不是真的。 Java/JSF and JavaScript does not run in the same environment. Java/JSF 和 JavaScript 不在同一环境中运行。 You should see Java/JSF more as a HTML/CSS/JS code generator.您应该将 Java/JSF 更多地视为 HTML/CSS/JS 代码生成器。 Java/JSF runs in webserver, generates HTML/CSS/JS and sends it to webbrowser. Java/JSF 在 webserver 中运行,生成 HTML/CSS/JS 并将其发送到 webbrowser。 HTML/CSS/JS runs in the webbrowser. HTML/CSS/JS 在网络浏览器中运行。

You need to let Java/JSF print the bundleAdded property value using EL.您需要让 Java/JSF 使用 EL 打印bundleAdded属性值。

onclick="if(this.checked != #{accountAdjustmentBean.bundleAdded}) ..."

I do not know JSF but IMHO it should be我不知道 JSF 但恕我直言它应该是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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