我的代码如下,
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"/>
我的背豆:
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.......
当我选中复选框时,没有警报。问题在哪里?我想不出来。有人能指出来吗?
谢谢!
您期望bundleAdded
属性作为onclick
函数作用域中的JavaScript变量神奇地呈现。这不是真的。Java/JSF和JavaScript不在同一个环境中运行。您应该更多地将Java/JSF视为HTML/CSS/JS代码生成器。Java/JSF运行在web服务器上,生成HTML/CSS/JS并发送给web浏览器。HTML/CSS/JS在浏览器中运行。
您需要让Java/JSF使用EL打印bundleAdded
属性值。
onclick="if(this.checked != #{accountAdjustmentBean.bundleAdded}) ..."
此条件无效:= bundleAdded)我想你只需要做:如果(this.checked)
如果你想看看是否值(bundleAdded)实际上已经设置,那么你不应该使用Javascript来做到这一点,因为这只是在客户端(除非你当然使用Ajax,但看看你的例子,我看不出你为什么要)。
我不知道JSF,但我认为应该是
if(this.checked && this.value=='bundleAdded'){alert('Worked');}else{alert('failed');}