0

在一个表单中有几个 primefaces inputtext 控件和命令按钮。在一个输入文本上按回车键需要激活相关命令按钮的单击。我怎样才能实现该功能?我什至尝试使用onkeydown,但找不到方法。

4

2 回答 2

1

RongNK的指导下,我将代码更改如下,它完美地满足了我的目的。

添加了 CDATA 以包含 JavaScript。使用 \\ 转义:在 JSF 组件的 ID 中

  <h:form id="frmEn">
                <script type="text/javascript" language="JavaScript">
                    //<![CDATA[    
                    function forDx(e) {
                        if (e.keyCode === 13) {
                            $('#frmEn\\:btnDx').click();
                            return false;
                        } else {
                            return true;
                        }
                    }
                    function forIx(e) {
                        if (e.keyCode === 13) {
                            $('#frmEn\\:btnIx').click();
                            return false;
                        } else {
                            return true;
                        }
                    }
                    function forMx(e) {
                        if (e.keyCode === 13) {
                            $('#frmEn\\:btnMx').click();
                            return false;
                        } else {
                            return true;
                        }
                    }
                    function forRx(e) {
                        if (e.keyCode === 13) {
                            $('#frmEn\\:btnRx').click();
                            return false;
                        } else {
                            return true;
                        }
                    }
                    // ]]>
                </script> 

                <p:panel header="Encounter" >
                    <h:panelGrid columns="2" >
                        <p:panel header="Encounter Details" >

                            <h:panelGrid columns="4" >


                                <h:outputLabel value="Tests" ></h:outputLabel>
                                <p:autoComplete  id="txtIx"  value="#{encounterController.test }" completeMethod="#{encounterController.completeIx}"  styleClass="defaultTxt" onkeydown="return forIx(event)" >
                                </p:autoComplete>
                                <h:commandButton id="btnIx"  value="Add" action="#{encounterController.addTest()}">
                                    <f:ajax  execute="btnIx txtIx" render="tblIx" />
                                </h:commandButton>
                                <p:dataTable value="#{encounterController.ecIxs }" var="ix" id="tblIx" >
                                    <p:column >
                                        <f:facet name="header" >
                                            <h:outputLabel value="Tests"/>
                                        </f:facet>
                                        <h:outputLabel value="#{ix.concept.name}"></h:outputLabel>
                                    </p:column>
                                </p:dataTable>






                                <h:outputLabel value="Diagnosis" ></h:outputLabel>
                                <p:autoComplete  id="txtDx"  value="#{encounterController.diagnosis }" completeMethod="#{encounterController.completeDx}"  styleClass="defaultTxt" onkeydown="return forDx(event)" />
                                <h:commandButton id="btnDx"  value="Add" action="#{encounterController.addDiagnosis()}" >
                                    <f:ajax  execute="btnDx txtDx" render="tblDx txtDx" />
                                </h:commandButton>
                                <p:dataTable value="#{encounterController.ecDxs }" var="dx" id="tblDx" >
                                    <p:column >
                                        <f:facet name="header" >
                                            <h:outputLabel value="Diagnoses"/>
                                        </f:facet>
                                        <h:outputLabel value="#{dx.concept.name}"></h:outputLabel>
                                    </p:column>
                                </p:dataTable>



                                <h:outputLabel value="Treatment" ></h:outputLabel>
                                <p:autoComplete  id="txtRx"  value="#{encounterController.rx}" completeMethod="#{encounterController.completeRx}"  styleClass="defaultTxt" onkeydown="return forRx(event)">
                                </p:autoComplete>

                                <h:commandButton id="btnRx"  value="Add" action="#{encounterController.addRx()}">
                                    <f:ajax  execute="btnRx txtRx" render="tblRx" />
                                </h:commandButton>
                                <p:dataTable value="#{encounterController.ecRxs }" var="rx" id="tblRx" >
                                    <p:column >
                                        <f:facet name="header" >
                                            <h:outputLabel value="Treatment"/>
                                        </f:facet>
                                        <h:outputLabel value="#{rx.concept.name}"></h:outputLabel>
                                    </p:column>
                                </p:dataTable>


                                <h:outputLabel value="Plan" ></h:outputLabel>
                                <p:autoComplete  id="txtMx"  value="#{encounterController.plan }" completeMethod="#{encounterController.completeMx}"  styleClass="defaultTxt" onkeydown="return forMx(event)">
                                </p:autoComplete>

                                <h:commandButton id="btnMx"  value="Add" action="#{encounterController.addPlanOfAction() }">
                                    <f:ajax  execute="btnMx txtMx" render="tblMx" />
                                </h:commandButton>
                                <p:dataTable value="#{encounterController.ecMxs}" var="mx" id="tblMx" >
                                    <p:column >
                                        <f:facet name="header" >
                                            <h:outputLabel value="Plan"/>
                                        </f:facet>
                                        <h:outputLabel value="#{mx.concept.name}"></h:outputLabel>
                                    </p:column>
                                </p:dataTable>


                                <h:outputLabel value="Details" ></h:outputLabel>
                                <h:inputTextarea value="#{encounterController.current.comments}" styleClass="defaultTxtArea"></h:inputTextarea>
                                <h:outputLabel value="" ></h:outputLabel>
                                <h:outputLabel value="" ></h:outputLabel>

                                <h:outputLabel value="Charges" ></h:outputLabel>
                                <h:inputTextarea value="#{encounterController.current.charge}" styleClass="defaultTxt"></h:inputTextarea>
                                <h:outputLabel value="" ></h:outputLabel>
                                <h:outputLabel value="" ></h:outputLabel>

                                <h:outputLabel value=""></h:outputLabel>
                                <h:commandButton value="Save" action="#{encounterController.saveSelected()}"></h:commandButton>
                                <h:outputLabel value="" ></h:outputLabel>
                                <h:outputLabel value="" ></h:outputLabel>
                            </h:panelGrid>


                        </p:panel>
                        <p:panel header="Patient Details" >
                            <h:panelGrid columns="2" >
                                <h:outputLabel value="Name"></h:outputLabel>
                                <h:outputLabel value="#{encounterController.current.patient.person.name}"></h:outputLabel>

                                <h:outputLabel value="Age"></h:outputLabel>
                                <h:outputLabel value="#{encounterController.current.patient.person.age}"></h:outputLabel>

                                <h:outputLabel value="Date of Birth"></h:outputLabel>
                                <h:outputLabel value="#{encounterController.current.patient.person.dob}">
                                    <f:convertDateTime pattern="dd MMMM yyyy" />
                                </h:outputLabel>

                                <h:outputLabel value="Sex"></h:outputLabel>
                                <h:outputLabel value="#{encounterController.current.patient.person.sex.name}"></h:outputLabel>

                                <h:outputLabel value="Address"></h:outputLabel>
                                <h:outputLabel value="#{encounterController.current.patient.person.address}"></h:outputLabel>

                            </h:panelGrid>
                        </p:panel>
                    </h:panelGrid>

                </p:panel>

            </h:form>
于 2013-04-12T03:11:42.370 回答
1

你可以试试(你需要自己找到在javascript中检测回车键)

xhtml:

<p:input onkeydown="test()" />
<p:commandButton styleClass="foo" />

javascript:

function test(){
$('.foo').click();
}

您还可以查看: How-to-programmatically-trigger-onclick-eventHow-to-refer-to-a-jsf-component-id-in-jquery

于 2013-04-12T02:20:18.753 回答