简体   繁体   English

在primefaces中使用javascript从selectOnemenu获取选定的值,然后打开一个对话框

[英]get selected value from selectOnemenu using javascript in primefaces and open a dialog box

How can we get the selected value of PrimeFaces <p:selectOneMenu> using JavaScript/jQuery? 我们如何使用JavaScript / jQuery获得PrimeFaces <p:selectOneMenu>的选定值?

I am trying to get it this way, but it does not go inside if condition, which means that the ID of the element is not correct. 我正在尝试以这种方式获取它,但是如果有条件,它就不会进入内部,这意味着该元素的ID不正确。

<h:head> 
    <script> 
        function showDialog() { 
            alert("insdie function"); 
            if($('#someSelect').val() == 'India') { 
                dlg.show(); 
                alert("after function"); 
            } 
            alert("outside function"); 
        }   
    </script> 
</h:head> 
<h:body> 
    <h:form> 
        <p:panel> 
            <h:panelGrid columns="2"> 
                <p:selectOneMenu 
                    id="someSelect" 
                    value="#{testController.countryName}" 
                    <f:selectItem itemLabel="Select One" itemValue="" /> 
                    <f:selectItems value="#{addPatientProfileBB.patStatusSelect}" 
                        itemLabel="#{testController.countryName}" 
                        itemValue="#{testController.countryNameId}" /> 
                    <p:ajax process="someSelect" update="dialog" oncomplete="showDialog()"/> 
                </p:selectOneMenu> 
            </h:panelGrid> 

            <p:dialog id="dialog" header="Login" widgetVar="dlg"> 
                <h:form> 
                    <h:panelGrid columns="2" cellpadding="5"> 
                        <h:outputLabel for="username" value="Username:" /> 
                        <p:inputText id="username" required="true" label="username" /> 
                    </h:panelGrid> 
                </h:form> 
            </p:dialog> 
        </p:panel>  
    </h:form> 
</h:body>

JSF runs on webserver and generates HTML which get sent to webbrowser. JSF在Web服务器上运行,并生成HTML,该HTML被发送到Webbrowser。 JavaScript/jQuery runs on webbrowser and doesn't see anything of the JSF source code, but only its HTML output. JavaScript / jQuery在Webbrowser上运行,看不到JSF源代码的任何内容,只能看到其HTML输出。

Open the page in browser, rightclick and View Source ( or here on PrimeFaces showcase site ). 在浏览器中打开页面,右键单击并查看源或在PrimeFaces展示站点上的此处 )。 You'll see that the actual <select> element has the ID of the parent <h:form> prepended and the word _input suffixed (because the <p:selectOneMenu> basically generates a <div><ul><li> to achieve the fancy look'n'feel which isn't possible with a plain <select> , thus it's been hidden away). 您会看到实际的<select>元素在父元素<h:form>的ID _input加上单词_input后缀(因为<p:selectOneMenu>基本上会生成<div><ul><li>普通的<select>无法实现的精美外观,因此已被隐藏起来)。

So, if you give the parent form a fixed ID (so that JSF doesn't autogenerate one), then the following JSF code 因此,如果给父表单一个固定的ID(这样JSF不会自动生成一个ID),那么下面的JSF代码

<h:form id="form">
    <p:selectOneMenu id="someSelect" ...>

will generate the HTML <select> as follows: 将生成HTML <select> ,如下所示:

<select id="form:someSelect_input">

You need to use exactly that ID instead to grab the element from DOM. 您需要完全使用该ID来从DOM中获取元素。

$("#form\\:someSelect_input");

or 要么

$("[id='form:someSelect_input']");

See also: 也可以看看:


Unrelated to the concrete problem, you've there another problem with that <p:dialog> . 具体问题无关 ,该<p:dialog>还有另一个问题。 It contains another <h:form> and thus you're effectively nesting forms which is illegal in HTML! 它包含另一个<h:form> ,因此您实际上是在嵌套HTML中非法的表单! Put that entire <p:dialog> outside the form like so: 将整个<p:dialog> 放在表单 ,如下所示:

<h:form>
    <p:selectOneMenu ... />
</h:form>
<p:dialog>
    <h:form>
        ...
    </h:form>
</p:dialog>

try changing 尝试改变

if($('#someSelect').val() == 'India') { 

into 进入

if($("select[name$='someSelect_input'] option:selected").val() == 'India') { 

EDIT 编辑

you can improve the selector by changing 您可以通过更改选择器来改进选择器

name$='someSelect_input'

into 进入

name='yourFormName\\:someSelect_input'

I my friends. 我是我的朋友们。 i found the following solution. 我发现以下解决方案。

<h:head> 
    <script> 
        function showDialog() {
            alert(PF('selectWV').getSelectedValue());
            if (PF('selectWV').getSelectedValue() == "b") {
                PF('buttonWV').jq.show();
            } else {
                PF('buttonWV').jq.hide();
            }
        }  
    </script> 
</h:head> 
<h:body> 
    <h:form> 
        <p:panel> 
            <h:panelGrid columns="2"> 
                <h:form>
            <p:selectOneMenu style="width:150px" id="id" widgetVar="selectWV">  
                <f:selectItem itemLabel="Select" itemValue="a"></f:selectItem>
                <f:selectItem itemLabel="Other" itemValue="b"></f:selectItem>
                <p:ajax process="id" update="dos" oncomplete="showDialog()"/>
            </p:selectOneMenu>
            <p:commandButton value="Register" widgetVar="buttonWV"
                style="display: none" />
        </h:form>
            </h:panelGrid> 
        </p:panel>  
    </h:form> 
</h:body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM