如何在类型重置时按 p:commandButton 重置 p:selectOneMenu 的值。我的代码如下
<h:form id="form">
<p:panelGrid columns="2" cellspacing="10" >
<f:facet name="header">Login</f:facet>
<p:outputLabel value="Username" />
<p:inputText value="#{user.username}" />
<p:outputLabel value="Password" />
<p:password value="#{user.password}"></p:password>
<p:outputLabel value="Locale" />
<p:selectOneMenu >
<f:selectItem itemValue="Select Country" itemLabel="Select Country" />
<f:selectItem itemValue="Poland" itemLabel="Poland"/>
</p:selectOneMenu>
<p:commandButton value="Submit"></p:commandButton>
<p:commandButton type="reset" value="Clear" update="form"></p:commandButton>
</p:panelGrid>
</h:form>
执行此操作后,用户名和密码将被清除,但选择国家/地区的下拉列表不会重置。
首先你只是展示你的价值观p:selectOneMenu
但不分配这些值,value
property 能够将客户端当前选择的值分配给支持 bean 值,因此;
<p:selectOneMenu id="myMenu" value="#{bean.selectedCountry}">
<f:selectItem itemValue="Select Country" itemLabel="Select Country" />
<f:selectItem itemValue="Poland" itemLabel="Poland"/>
</p:selectOneMenu>
现在,如果用户选择波兰作为国家/地区,它将被设置为selectedCountry
在支持 bean 上也不要忘记实现 getter 和 setter 方法。
那么如果你想重置组件的值,p:selectOneMenu
生成一个标签并更改它的文本可以在视图上实现这一点:
<p:commandButton onclick="resetter();" type="reset" value="Clear" update="form"></p:commandButton>
以及js函数:
function resetter() {
document.getElementById('form:myMenu_label').innerHTML = 'Select Country';
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)