简体   繁体   English

在JavaScript中获取p:selectOneMenu的选定值

[英]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.

相关问题 如何在jquery中获取<p:selectOneMenu>的值? - How to get the value of <p:selectOneMenu> in jquery? 如何使用JavaScript获取按钮的选定值 - How to fetch selected value of button using javascript 在primefaces中使用javascript从selectOnemenu获取选定的值,然后打开一个对话框 - get selected value from selectOnemenu using javascript in primefaces and open a dialog box 读取客户端的价值 <p:selectOneMenu/> 在onchange回调中 - Reading client-side value of <p:selectOneMenu/> in onchange callback 使用javascript更改primefaces selectonemenu标签/值 - change primefaces selectonemenu label / value with javascript 选择h:selectOneMenu的特定项时,调用特定的JavaScript函数 - Call specific JavaScript function when specific item of h:selectOneMenu is selected 如何从中获取所选项目 <h:selectOneMenu> 在JavaScript中 - How to get selected item from <h:selectOneMenu> in javascript 如何使用JavaScript将多个下拉列表选择的值提取到单个数组中 - How to fetch multiple dropdowns selected value into a single array using JavaScript 提取作者[AEM]在Javascript中选择的对话框属性值 - To fetch dialog property value selected by an author [AEM] in Javascript 无法使用JavaScript在下拉框中获取选定的值 - Cannot fetch the selected value in a drop down box using JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM