javascript 如何在jquery中获取<p:selectOneMenu>的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8093432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:14:28  来源:igfitidea点击:

How to get the value of <p:selectOneMenu> in jquery?

javascriptjqueryjsfjsf-2primefaces

提问by Karthikeyan

Hi am trying to get the value of p:selectOneMenu from jquery , but i dint got as yet. am using JSF and primefaces as my UI component.

嗨,我正在尝试从 jquery 获取 p:selectOneMenu 的值,但我还没有得到。我使用 JSF 和 primefaces 作为我的 UI 组件。

    <p:selectOneMenu style="width:150px" id="skill"
                value="#{loginBean.skill}" required="true" immediate="true"
                requiredMessage="Select your skill" label="skill" styleClass="someClassName">
                <f:selectItem itemLabel="Select" itemValue=""></f:selectItem>
                <f:selectItem itemLabel="Other" itemValue="Other"></f:selectItem>
                <f:selectItems value="#{loginBean.skillList}" var="item"
                    itemLabel="#{item}" itemValue="#{item}"></f:selectItems>
            </p:selectOneMenu>

In Html

在 HTML 中

<select id="skill_input" name="skill_input">
<option value="">Select</option>
<option value="Other">Other</option>
<option value="Tailoring">Tailoring</option>
<option value="Swimming">Swimming</option>
<option value="Roaming">Roaming</option>
</select>

thats my select one menu, my js is..

那是我选择的一个菜单,我的 js 是..

  var $element = $('.someClassName');

alert($element);

in the alert box i got as, [object Object], but not the selected value.

在我得到的警报框中,[object Object],但不是选定的值。

then i tried this,

然后我尝试了这个,

   var $element = $('.someClassName').val();

alert($element);

but now i got an empty alert box.

但现在我有一个空的警报框。

then i tried this

然后我尝试了这个

 var $element = $("select[name='skill_input']:selected").val();

alert($element);

my alert box says undefined

我的警告框说未定义

What else i should do to get the selected value in that alert box..??

我还应该怎么做才能在那个警报框中获得选定的值..??

回答by dereli

Try this

试试这个

var $element = $("select[name='skill_input'] option:selected").val();

alert($element);

回答by spauny

I haven't really used Jquery but if you want to use a simple javascript solution, that works, you're welcome to:

我还没有真正使用过 Jquery,但是如果你想使用一个简单的 javascript 解决方案,那么它的工作原理,欢迎你:

var selectedValue = document.getElementById("MyForm:skill").value

MyFormwould be the form id where selectOneMenu component can be found. And skill, of course is you selectOneMenu id.

MyForm将是可以找到 selectOneMenu 组件的表单 ID。而且skill,当然是你selectOneMenu id。

回答by Irishka

you need to select one option which value is not empty, or set first option value to something like 'empty'

您需要选择一个值不为空的选项,或将第一个选项值设置为“空”之类的值

  • by default the first option is selected and in your code it has an empty value - so you get nothing displayed
  • 默认情况下,第一个选项被选中,在你的代码中它有一个空值 - 所以你什么也没有显示

回答by Valijon

Define widgetVarand get value with PrimeFace framework

widgetVar使用 PrimeFace 框架定义并获取价值

<p:selectOneMenu widgetVar="foo" style="width:150px" id="skill"
    value="#{loginBean.skill}" required="true" immediate="true"
    requiredMessage="Select your skill" label="skill" styleClass="someClassName">
        <f:selectItem itemLabel="Select" itemValue=""></f:selectItem>
        <f:selectItem itemLabel="Other" itemValue="Other"></f:selectItem>
        <f:selectItems value="#{loginBean.skillList}" var="item"
                    itemLabel="#{item}" itemValue="#{item}">
        </f:selectItems>
</p:selectOneMenu>

// Call it in your JS
console.log(PF('foo').getSelectedValue());