简体   繁体   English

如何通过使用JavaScript获得JSF selectBooleanCheckbox的值?

[英]How to get a value of JSF selectBooleanCheckbox by using JavaScript?

I save in my selectBooleanCheckbox only boolean values in a backingbean which named "bean". 我在一个名为“ bean”的backingbean中仅将布尔值保存在selectBooleanCheckbox中。 To set a label for this checkbox in my JSF xhtml page i´m using a JSF outputText component. 要在我的JSF xhtml页面中为此复选框设置标签,请使用JSF outputText组件。

Here is the JSF code (they are 2 checkboxes in this example): 这是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"/>

Now i want to get the selected checkboxes with java script. 现在,我想使用Java脚本获取选定的复选框。 I´m using the onselect event: 我正在使用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"/>

But this gets only the boolean values of the selected checkboxes. 但这只会获取所选复选框的布尔值。 Is it possible to get the value of the outputText components? 是否可以获取outputText组件的值? Is tehre any way to connect the output component with the selectBooleanCheckbox component? 有什么办法可以将输出组件与selectBooleanCheckbox组件连接起来?

You should give a unique ID to the components and then try getting these values in java script by their ID , Also you can use 'onclick' to call the java script method. 您应该为组件赋予唯一的ID,然后尝试通过其ID在Java脚本中获取这些值,也可以使用“ onclick”调用Java脚本方法。 for ex: 例如:

<h:selectBooleanCheckbox id="firstBox" class="extern" value="#{bean.booleanValue1}"       onclick="update(this)"/>
<h:outputText id="firstOutput" value="My Labeltext1"/>

And your java script function would look something like this: 而且您的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.

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