要求:有一个文本为“禁用”的按钮。我希望在页面打开时禁用它。当用户点击按钮时,我希望我的表单字段启用,同时我希望我的按钮值更改为“启用”。
我有一个如何更改按钮值的示例,但如何同时启用/禁用它?
<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 :(得分: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;
}