问题描述
我正在尝试使用javascript在selectOneMenu中预先选择用户所在的国家/地区。
问题是当页面最初加载时,列表仍然在第一个元素上。但是,如果我单击箭头以展开列表,然后单击页面上的其他位置以使列表消失,则值是javascript设置的值。
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>
和javascript:
and the javascript:
<script language="JavaScript" src="http://www.geoplugin.net/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";
}
}
}
推荐答案
我不确定目标是什么,但答案是:
I'm not sure what's the goal but the answer is this:
按价值选择:
PF('selectOneMenuWV').selectValue('3')
按标签选择:
PF('selectOneMenuWV').selectItem(PF('selectOneMenuWV').jq.find('.ui-selectonemenu-item[data-label*="Option Label"]'))
请注意,您应该定义一个widgetVar,在此示例中为selectOneMenuWV。
Note that you should define a widgetVar, which is selectOneMenuWV in this example.
<p:selectOneMenu ... widgetVar="selectOneMenuWV">
这篇关于使用JS在p:selectOneMenu中选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!