JSF richfaces getElementById

那年仲夏 提交于 2019-12-11 08:20:19

问题


I am using jsf2 and richfaces 4.X how can i get the richfaces enabled input field's current value ? equal to getElementById('field_name').vlue

I tried some of the methods named like findcomponent.value and element.value but they give me old value which was while page was loaded b/c findcomponent and element method returns server side UI.components instead client side components ?


回答1:


I guess you are to use rich:clientId function to render a correct identifier into your java script.

For example:

var myElement = document.getElementById("#{rich:clientId('field_name')}");

See also RichFaces rich:clientId within facelets




回答2:


If you check the HTML generated, you could see that every JSF / RF element has his id like :. For example:

<h:form id="frmSample">
    <h:inputText id="txtSample" value="#{mybean.someTextValue}" />
</h:form>

The HTML generated will be:

<form method="POST">
    <input type="text" id="frmSample:txtSample" />
</form>

So, in JavaScript you could reference the element by this id.

var txtValue = document.getElementById('frmSample:txtSample').value;

Also, for the RF composite HTML components like rich:tab or rich:calendar you could use the component ID generated by HTML but I'll recommend "#{rich:clientId('field_name')}" as @DRCB has explained in his post.




回答3:


To get the richfaces components values use the rich:component and the getValue() methods.
To get the jsf library components values use the rich:element and the value JS methods.

Example:

<h:form>
     <h:inputText id="textField" value="#{bean.value1}" />
     <a4j:commandButton value="displayTextField" action="doSomething()"
       onclick="alert('v:'+ #{rich:element('textField')}.value); return true;" />

     <rich:inplaceInput id="richField" value="#{bean.value1}" />
     <a4j:commandButton value="displayRichField" action="doSomething()"
       onclick="alert('v:'+ #{rich:component('richField')}.getValue()); return true;" />
</h:form>

They both work even in a rich:dataTable, without adding the id of the datatable or something. Richfaces is quite smart and find the ids of the 'current row' example:

<h:form>
     <rich:dataTable id="mydatatable" value="bean.findValues()" var="myvar">

         <rich:column>

              <h:inputText id="textField" value="#{myvar.text1}" />
              <a4j:commandButton value="displayTextField" action="doSomething()"
                   onclick="alert('v:'+ #{rich:element('textField')}.value); return true;" />
         </rich:column>

         <rich:column>

             <rich:inplaceInput id="richField" value="#{myvar.text2}" />
             <a4j:commandButton value="displayRichField" action="doSomething()"
                  onclick="alert('v:'+ #{rich:component('richField')}.getValue()); return true;" />
         </rich:column>

     </rich:dataTable>
</h:form>


来源:https://stackoverflow.com/questions/10137170/jsf-richfaces-getelementbyid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!