简体   繁体   English

使用javascript更改primefaces selectonemenu标签/值

[英]change primefaces selectonemenu label / value with javascript

i have a textfield and a few selectonemenus. 我有一个文本字段和一些selectonemenus。 If i write something into the textfield, i want to update the selected items / the label of the dropdowns and the other way around: 如果我在文本字段中写一些东西,我想更新所选的项/下拉菜单的标签,反之亦然:

在此处输入图片说明

The problem is i can't access the dropdown label from the textfield event. 问题是我无法从textfield事件访问下拉标签。

JSF for the textfield: 文本字段的JSF:

<p:inputText id="nameInput"value="#{searchProject.searchName}" onkeyup="textToDropdown()"/>

JSF for the selectonemenu: JSF for selectone菜单:

<p:selectOneMenu id="brand" value="#{searchProject.brand}">
    <f:selectItem itemLabel="" itemValue="" />
    <f:selectItems value="#{createProject.brands}" var="i" itemLabel="#{i.value} - #{i.label}" itemValue="#{i}"/>
    <f:converter binding="#{AttributeConverter}" />
</p:selectOneMenu>

My Javascript so far: 到目前为止,我的Javascript:

<script type="text/javascript">
    function textToDropdown(){
    var x = document.getElementById("selectionForm:entryActionTabs:namePatternInput");
    //if the text of the input is bigger than 2, fill the first dropdown        
    if(x.value.length >= 2)
        {
            //I DONT KNOW HOW DO ACCESS THE LABEL OF THE DROPDOWN
        }
    }
</script>

So what did i try to change the label ? 那我想怎么改变标签呢? I checked the DOM and found a label underneath the dropdown with a id: 我检查了DOM,并在下拉菜单下找到了一个带有ID的标签:

picture of the DOM DOM的图片

Therefore i tried to access the label directly, with javascript to change the text: 因此,我尝试使用javascript直接访问标签来更改文本:

var label= document.getElementById("selectionForm:entryActionTabs:brand_selection2_label");

But i only get a undefined object back...my second approach would be to use the .innerHtml but how to i access the label with that ? 但是我只能得到一个未定义的对象...我的第二种方法是使用.innerHtml,但是我该如何访问标签呢? there are also the three other div-classes included. 还包括其他三个div类。

Got the label with: 使用以下标签:

var label1 = document.getElementById("selectionForm:entryActionTabs:brand_selection2").getElementsByTagName("label")[0];                
            label1.innerHTML="HI";

but i still would like to know why i cant use the full-ID directly and if there is a better way to get the label. 但是我仍然想知道为什么我不能直接使用完整ID,以及是否有更好的方法来获取标签。 I'm sure there is one. 我确定有一个。

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

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