启用/禁用表单元素并一次更改按钮的值?

时间:2014-02-18 05:00:54

标签: javascript jquery jsf jsf-2 primefaces

要求:有一个文本为“禁用”的按钮。我希望在页面打开时禁用它。当用户点击按钮时,我希望我的表单字段启用,同时我希望我的按钮值更改为“启用”。

我有一个如何更改按钮值的示例,但如何同时启用/禁用它?

<h:form>
  Please press the Button it.
  <h:commandButton id="cb1" value="#{ActionBean.buttonText}"
    immediate="true" actionListener="#{ActionBean.processMyAction}" />
</h:form>

我的bean类看起来像这样:

/* Action listener method */
public void processMyAction(ActionEvent event) {
    disable = !disable;
    System.out.println("disable: " + disable);
    if (buttonText.equals("Disable")) {
        buttonText = "Enable";
        System.out.println("buttonText: " + buttonText);
    } else {
        buttonText = "Disable";
        System.out.println("buttonText: " + buttonText);
    }

1 个答案:

答案 0 :(得分:1)

以下是您尝试的示例。

    <h:body>
        <h:form>
           <h:inputText disabled="#{myBean.disabled}"></h:inputText>
           <h:commandButton value="#{myBean.buttonName}" actionListener="#{myBean.changeButton}"></h:commandButton>
        </h:form>
    </h:body>

豆子,

public class MyBean {    
    public MyBean(){
       this.disabled = true;
       this.buttonName = "Edit";
    }

    public String getButtonName() {
       return buttonName;
    }

    public void setButtonName(String buttonName) {
          this.buttonName = buttonName;
    }
    private String buttonName;

    public void changeButton(ActionEvent event){
        if(this.buttonName.equals("Edit")){
            this.disabled = false;
            this.buttonName = "Save";
        }else{
            this.disabled = true;
            this.buttonName = "Edit";
        }       
    } 
     public boolean isDisabled() {
         return disabled;
     }

     public void setDisabled(boolean disabled) {
          this.disabled = disabled;
     }

     private boolean disabled;   
}
相关问题