2014-02-18 31 views
1

要求:有一個文本「禁用」的按鈕。我希望頁面打開時禁用它。當用戶點擊按鈕時,我希望我的表單域被啓用,同時我希望我的按鈕值被更改爲「啓用」。啓用/禁用表單元素並一次更改按鈕的值?

我有一個如何改變按鈕的值的例子,但我該如何同時啓用/禁用它?

<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); 
    } 
+0

你的要求不明確。你想要一個按鈕來禁用文本,並禁用該按鈕? – Adarsh

+0

@Adarsh我想我在編輯中搞砸了。思考它的意思是說:「我希望它在頁面打開時說'禁用'」,即用戶單擊該按鈕,然後將其禁用並將其值更改爲「啓用」(至少這是唯一的方法合理)。 – mabi

+0

@mabi在這種情況下,在顯示啓用按鈕後將不再有任何用途,因爲按鈕本身將被禁用。這並沒有真正證實他向我們提供的代碼。 – Adarsh

回答

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> 

和bean,

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; 
} 
+0

未找到方法錯誤即將到來164b321.changeButton() – user3227175

+0

檢查bean中和jsf頁面上的方法名稱。它對我來說工作正常 – Adarsh

+0

當編輯按鈕存在時,我的表單字段被禁用 < h:outputLabel value =「Password」/> user3227175

相關問題