【发布时间】:2014-09-28 21:43:38
【问题描述】:
我需要在 javascript 中获取 <p:selectOneMenu> 的值,但我得到的结果是 null。我可以调用该函数,但是当我尝试访问<p:selectOneMenu> 的值时。我在警告框中得到空值。
请找到下面的代码。
<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 validateYearMonth()
{
var yearValue1=document.getElementById('Form:year_input');
var yearValue2=document.getElementById('Form:year');
alert(yearValue1);
alert(yearValue2);
}
调用函数:
<p:commandButton id="Submit" action="#{someBean.functionName}"
onclick="validateYearMonth()" value="Submit" ajax="false" style="float:right;">
</p:commandButton>
我不明白怎么了。 虽然我可以获取元素如果我正在做
var yearValue1=document.getElementById('Form:year_input');
但我无法获取值。非常感谢任何帮助。
【问题讨论】:
-
@BalusC:我尝试了你的方法..我已经用你提供的解决方案更新了我的问题,但即使对他们来说我也得到了空值。 Year 是我的实体 ID,year_input 是我在查看源代码后得到的 id。为什么我收到 null?
-
@BalusC :不,根据所做的更改,我没有得到未定义。但是我在 validateYearMonth 函数中提到的两个警报中都为空。
-
@BalusC :我能做些什么来解决这个问题吗?请指导。
标签: javascript jsf jsf-2 primefaces selectonemenu