1

我有以下JSF代码:

<p:selectOneRadio id="contactPersonGender" value="#{myBean.contactPersonGender}"> 
    <f:selectItems value="#{data.getGenders()}" var="gender" itemValue="#{gender}" itemLabel="#{gender.label}" />
</p:selectOneRadio>

<p:commandButton onclick="copyFromContactToManager();" ajax="false" type="button" update="@form"/>

<p:selectOneRadio id="managerGender" value="#{myBean.managerGender}">   
        <f:selectItems value="#{data.getGenders()}" var="gender" itemValue="#{gender}" itemLabel="#{gender.label}" />
</p:selectOneRadio>

我现在想在单击按钮时将选择从 contactPersonGender 复制到 managerGender。data.getGenders() 返回枚举类型“性别”(MALE 或 FEMALE)的数组。因此,例如,当在 contactPersonGender 中选择 FEMALE 时,当单击按钮时 managerGender 也应该选择 FEMALE。

我尝试了以下代码:

function copyFromContactToManager(){
    $("input[id^='" + contactPersonGender + "']").each(function(index, element) {
        if ($(element).prop("checked")) {
                $("input[id^='managerGender:" + index + "']").first().click();
        }
    });
}

或者:

function copyFromContactToManager(){
    $("input[id^='" + contactPersonGender + "']").each(function(index, element) {
        $("input[id^='managerGender:" + index + "']").prop("checked", $(element).prop("checked"));
    }
}

它们都可以工作,但是在单击我的按钮后 UI 没有更新(例如,如果在 contactPersonGender 中选择了“MALE”并且在 managerGender 中选择了“FEMALE”,则在单击 managerGender 中的按钮后“FEMALE”似乎仍然被选中。但如果我将表单发送到服务器并保存数据,managerGender 的预期值为“MALE”)。

4

1 回答 1

2

您正在尝试做一个简单的工作,但您拥有的代码是无关紧要的。您不需要任何 JS 来执行此操作。

看看我在这里提供的例子

XHTML 代码

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">

<h:head>


</h:head>


<h:body>

    <style media="screen" type="text/css">
.ui-widget,.ui-widget .ui-widget {
    font-size: 90% !important;
}
</style>


    <h:form>
        <p:panel header="Contact Person">
            <h:panelGrid columns="2">
                <h:outputText value="Select Gender"></h:outputText>
                <p:selectOneRadio id="contactPersonGender"
                    value="#{testBean.contactPersonGender}">
                    <f:selectItems value="#{testBean.genders}" var="gender"
                        itemLabel="#{gender.name}" />
                </p:selectOneRadio>
            </h:panelGrid>
        </p:panel>
        <p:separator></p:separator>
        <p:commandButton value="Copy to Manager"
            actionListener="#{testBean.copyToManager}" update="managerPanel"></p:commandButton>
        <p:separator></p:separator>
        <p:panel header="Manager Person" id="managerPanel">
            <h:panelGrid columns="2">
                <h:outputText value="Select Gender"></h:outputText>
                <p:selectOneRadio id="managerGender"
                    value="#{testBean.managerGender}">
                    <f:selectItems value="#{testBean.genders}" var="gender"
                        itemLabel="#{gender.name}" />
                </p:selectOneRadio>
            </h:panelGrid>
        </p:panel>


    </h:form>

</h:body>
</html>

托管豆

package reg.bean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;

@ManagedBean(name = "testBean")
@ViewScoped
public class TestBean {

    private GENDER contactPersonGender;
    private GENDER managerGender;
    private GENDER[] genders;

    public TestBean() {
        genders = GENDER.values();
    }

    public void processForm(ActionEvent event) {
        System.out.println("Contact person Gender " + contactPersonGender.getName());
        System.out.println("Manager Gender " + managerGender.getName());
    }

    public void copyToManager(ActionEvent event){
        this.managerGender=this.contactPersonGender;
    }

    public GENDER[] getGenders() {
        return genders;
    }

    public void setGenders(GENDER[] genders) {
        this.genders = genders;
    }

    public GENDER getContactPersonGender() {
        return contactPersonGender;
    }

    public void setContactPersonGender(GENDER contactPersonGender) {
        this.contactPersonGender = contactPersonGender;
    }

    public GENDER getManagerGender() {
        return managerGender;
    }

    public void setManagerGender(GENDER managerGender) {
        this.managerGender = managerGender;
    }
}

性别类枚举

package reg.bean;

public enum GENDER {
    MALE("Male"), FEMALE("Female");
    private String name;

    GENDER(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }
}

现在,如果您在联系人中选择 MALE,在经理中选择 FEMALE,并且如果您按下按钮,那么经理将是 FEMALE。用户界面将被更新。

输出

于 2014-02-09T16:09:56.510 回答