简体   繁体   English

如何在javascript中获取primefaces selectOneMenu组件

[英]how to get primefaces selectOneMenu component in javascript

I have a lookup with primefaces components like this; 我用这样的素面组件进行查找;

<p:selectOneMenu id="selectOneMenuProjects"
    value="#{operationMessageBean.selectedModel.operationMessageProject}">
<f:selectItems id="selectItemProjects"
    value="#{selectItemBean.operationMessageProjectItemsAsObject}" />
</p:selectOneMenu>

I want to get "selectItems" component in my javascript method like this; 我想在我的javascript方法中获得“ selectItems”组件;

<script type="text/javascript">
  function getComponent() {
     var nodeName = document
       .getElementById('selectItemProjects').nodeName;
  }
</script>

BUT document.getElementById('selectItemProjects').nodeName; 但是document.getElementById('selectItemProjects').nodeName; returns null 返回null

How can I get this "selectItems" component ? 如何获得此“ selectItems”组件?

Can you use jQuery? 可以使用jQuery吗?

If yes, the solution is easy, you just have to write something like this: 如果是,则解决方案很简单,您只需要编写如下内容:

jQuery("[id*='selectOneMenuProjects']");

This must return the component and you can work with it. 这必须返回组件,您可以使用它。 Note that I have used *= (contains) because JSF generates ids with self-identifiers before the id typed by you. 请注意,我使用* =(包含)是因为JSF会在您键入的ID之前生成带有自我标识符的ID。 If you can't use jQuery I think in something like this: 如果您不能使用jQuery,我会认为是这样的:

var i=0, div;
while(div= document.getElementsByTagName('div')[i++]){
    if(div.id.match(/selectOneMenuProjects/)){

    }
}

Or instead of div, whatever Primefaces generates as HTML code. 还是用div代替Primefaces生成的任何HTML代码。

Hope it helps. 希望能帮助到你。

Be careful using the ID of any component in JSF, sometimes the ID name get nested with the parent name. 小心使用JSF中任何组件的ID,有时ID名称会与父名称嵌套在一起。 Example: A selectOneMenu(ID=select12) inside a form, could have the name form:select12_input when rendered in your page. 示例:表单内的selectOneMenu(ID = select12)在页面中呈现时,其名称可能为form:select12_input

I suggest you to watch the rendered name of your component using FireBug or Firefox/Chrome Inspector. 建议您使用FireBug或Firefox / Chrome Inspector观看组件的渲染名称。

Solutions: 解决方案:

  • If you want to know the .nodeName based on some action of selectOneMenu(onchange for example), you could this: 如果您想基于selectOneMenu的某些操作(例如onchange)来了解.nodeName,则可以执行以下操作:

<p:selectOneMenu id="cmbPais" onchange="yourFunction(this); ......

Now in your JS: 现在在您的JS中:

   <script type="text/javascript">
        function yourFunction(a) {
           alert(a.nodeName);
        }
   </script>
  • If you want just to get the nodeName in any function, inspect for the real name of your component... the code below works for me, but that depends on how your page was built. 如果只想获取任何函数中的nodeName,请检查组件的真实名称...以下代码对我有用,但这取决于页面的构建方式。

     <script type="text/javascript"> function test() { var nodeName = document .getElementById('form:selectOneMenuProjects_input').nodeName; alert(nodeName); } </script> 

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

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