![](/img/trans.png)
[英]How to set value of p:selectBooleanCheckbox with JavaScript
[英]How to get a value of JSF selectBooleanCheckbox by using JavaScript?
我在一個名為“ bean”的backingbean中僅將布爾值保存在selectBooleanCheckbox中。 要在我的JSF xhtml頁面中為此復選框設置標簽,請使用JSF outputText組件。
這是JSF代碼(此示例中有2個復選框):
<h:selectBooleanCheckbox class="extern" value="#{bean.booleanValue1}" />
<h:outputText value="My Labeltext1"/>
<h:selectBooleanCheckbox class="extern" value="#{bean.booleanValue2}" />
<h:outputText value="My Labeltext2"/>
現在,我想使用Java腳本獲取選定的復選框。 我正在使用onselect事件:
<h:selectBooleanCheckbox class="extern" value="#{bean.booleanValue1}" onselect="MyObject.update(this.options[this.selectedIndex].text);"/>
<h:outputText value="My Labeltext1"/>
<h:selectBooleanCheckbox class="extern" value="#{bean.booleanValue2}" onselect="MyObject.update(this.options[this.selectedIndex].text);"/>
<h:outputText value="My Labeltext2"/>
但這只會獲取所選復選框的布爾值。 是否可以獲取outputText組件的值? 有什么辦法可以將輸出組件與selectBooleanCheckbox組件連接起來?
您應該為組件賦予唯一的ID,然后嘗試通過其ID在Java腳本中獲取這些值,也可以使用“ onclick”調用Java腳本方法。 例如:
<h:selectBooleanCheckbox id="firstBox" class="extern" value="#{bean.booleanValue1}" onclick="update(this)"/>
<h:outputText id="firstOutput" value="My Labeltext1"/>
而且您的Java腳本函數看起來像這樣:
function update(val){
var box= document.getElementById("firstBox");
var outPut = document.getElementById("firstOutput");
var boxValue = box.value;
var outPutValue = outPut.value;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.