最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Selecting option in p:selectOneMenu using JS - Stack Overflow

programmeradmin2浏览0评论

I'm trying to pre-select the country of user in a selectOneMenu using javascript. The problem is that when the page initially loads the list is still on the first element. However if I click on the arrow to expand the list, and then click somewhere else on the page to make the list disappear, then the value is the one set by javascript.

<p:selectOneMenu styleClass="inputField selectOneMenuColored" required="true" editable="false" id="country" value="#{subscribeUser.user.countryBean}" converter="#{countriesConverter}" effect="fold">
   <f:selectItems value="#{countriesConverter.countries}" var="country" itemLabel="#{country.shortName}" itemValue="#{country}" />
</p:selectOneMenu>

and the javascript:

<script language="JavaScript" src=".gp" type="text/javascript"></script>
<h:body id="whole" onload="setSelectedIndex('form:country_input',geoplugin_countryName());">
...
function setSelectedIndex(menuId, val){
    var menu = document.getElementById(menuId);
    var opts = menu.options;
    for( j = 0; j < opts.length; j++) {
        if(opts[j].value == val) {
            opts[j].selected= "true";
        }
    }
}

I'm trying to pre-select the country of user in a selectOneMenu using javascript. The problem is that when the page initially loads the list is still on the first element. However if I click on the arrow to expand the list, and then click somewhere else on the page to make the list disappear, then the value is the one set by javascript.

<p:selectOneMenu styleClass="inputField selectOneMenuColored" required="true" editable="false" id="country" value="#{subscribeUser.user.countryBean}" converter="#{countriesConverter}" effect="fold">
   <f:selectItems value="#{countriesConverter.countries}" var="country" itemLabel="#{country.shortName}" itemValue="#{country}" />
</p:selectOneMenu>

and the javascript:

<script language="JavaScript" src="http://www.geoplugin/javascript.gp" type="text/javascript"></script>
<h:body id="whole" onload="setSelectedIndex('form:country_input',geoplugin_countryName());">
...
function setSelectedIndex(menuId, val){
    var menu = document.getElementById(menuId);
    var opts = menu.options;
    for( j = 0; j < opts.length; j++) {
        if(opts[j].value == val) {
            opts[j].selected= "true";
        }
    }
}
Share edited Aug 7, 2015 at 7:55 BalusC 1.1m376 gold badges3.7k silver badges3.6k bronze badges asked Aug 6, 2015 at 22:11 CedCed 17.5k15 gold badges100 silver badges156 bronze badges 15
  • One question: Why do you try this in this plicated way? – Kukeltje Commented Aug 7, 2015 at 1:46
  • One response: because I'm stupid and don't know the easy way. – Ced Commented Aug 7, 2015 at 4:59
  • Setting a value in the #{subscribeUser.user.countryBean} bean? – Kukeltje Commented Aug 7, 2015 at 6:24
  • Or using the PrimeFaces javascript api – Kukeltje Commented Aug 7, 2015 at 6:36
  • @HatemAlimam: And it was an honest question (ok maybe formulated less optimally from my mobile phone). There might have been a very valid reason todo it client side… although doing it via the javascript api is client side to. Always wonder why people do not use documentation or even know that these 'plex' jsf ponents have a client-side api#{subscribeUser.user.countryBean} – Kukeltje Commented Aug 7, 2015 at 7:44
 |  Show 10 more ments

1 Answer 1

Reset to default 6

I'm not sure what's the goal but the answer is this:

Select by value:

PF('selectOneMenuWV').selectValue('3')

Select by label:

PF('selectOneMenuWV').selectItem(PF('selectOneMenuWV').jq.find('.ui-selectonemenu-item[data-label*="Option Label"]'))

Note that you should define a widgetVar, which is selectOneMenuWV in this example.

<p:selectOneMenu ... widgetVar="selectOneMenuWV">
发布评论

评论列表(0)

  1. 暂无评论