[英]Fetch selected value of p:selectOneMenu in JavaScript
i need to fetch a value of a <p:selectOneMenu>
in javascript but the result that i get is null . 我需要在javascript中获取<p:selectOneMenu>
的值,但是我得到的结果为null 。 I am able to call the function but when i am trying to access the value of the <p:selectOneMenu>
. 我可以调用该函数,但是当我尝试访问<p:selectOneMenu>
的值时。 i get null in alert box. 我在警报框中得到空值。
Please find the below code. 请找到下面的代码。
<p:outputLabel for="year" value="Year: " />
<p:selectOneMenu id="year" style="width:150px " value="#{someBean.year}">
<f:selectItem itemLabel="All" itemValue="All"/>
<f:selectItem itemLabel="2014" itemValue="2014"/>
<f:selectItem itemLabel="2013" itemValue="2013"/>
<f:selectItem itemLabel="2012" itemValue="2012"/>
<f:selectItem itemLabel="2011" itemValue="2011"/>
<f:selectItem itemLabel="2010" itemValue="2010"/>
<f:selectItem itemLabel="2009" itemValue="2009"/>
</p:selectOneMenu>
Javascript function: JavaScript函数:
function validateYearMonth()
{
var yearValue1=document.getElementById('Form:year_input');
var yearValue2=document.getElementById('Form:year');
alert(yearValue1);
alert(yearValue2);
}
Call to function: 调用函数:
<p:commandButton id="Submit" action="#{someBean.functionName}"
onclick="validateYearMonth()" value="Submit" ajax="false" style="float:right;">
</p:commandButton>
I dont understand whats wrong. 我不明白怎么了。 while i am able fetch the element if i am doing 虽然我可以在获取元素的同时
var yearValue1=document.getElementById('Form:year_input');
but i am not able to fetch the value. 但我无法获取价值。 Any help much appreciated. 任何帮助,不胜感激。
A very simple approach would be setting a widgetVar
name and then get the current value! 一种非常简单的方法是设置一个widgetVar
名称,然后获取当前值!
<p:selectOneMenu widgetVar="yearWV">
in javascript (PF 4 and higher) 在javascript中 (PF 4及更高版本)
PF('yearWV').value
You can test this right away in your console 您可以在控制台中立即进行测试
you have to add return keyword in onclick event. 您必须在onclick事件中添加return关键字。 Eg. 例如。
<p:commandButton id="Submit" action="#{someBean.functionName}"
onclick="return validateYearMonth()" value="Submit" ajax="false" style="float:right;">
</p:commandButton>
**BEST WORKING SOLUTION :**
<p:selectOneMenu id="year" style="width:150px" value="#{someBean.year}" widgetVar="test" onchange="myTestFunction()">
<f:selectItem itemLabel="All" itemValue="All"/>
<f:selectItem itemLabel="2014" itemValue="2014"/>
<f:selectItem itemLabel="2013" itemValue="2013"/>
<f:selectItem itemLabel="2012" itemValue="2012"/>
<f:selectItem itemLabel="2011" itemValue="2011"/>
<f:selectItem itemLabel="2010" itemValue="2010"/>
<f:selectItem itemLabel="2009" itemValue="2009"/>
</p:selectOneMenu>
Java Script Code:
function myTestFunction()
{
var selectedValue=PF('test').getSelectedValue();
alert(selectedValue);
}
[**For more information please check this link ** https://forum.primefaces.org/viewtopic.php?t=39793][1]
In your JSF form, set prependId=false. 在您的JSF表单中,设置prependId = false。 ie 即
<h:form id="myform" prependId="false">
If you do not set prependId to false, what you get in your generated HTML dropdown's id will be 如果未将prependId设置为false,则生成的HTML下拉列表ID中的内容将是
<select id="myform:year">
In this case, in your javascript you have to get the element using document.getElementById("myform:year"); 在这种情况下,您必须在JavaScript中使用document.getElementById(“ myform:year”);获得元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.