Javascript 启用/禁用表单元素并一次更改按钮的值?
要求:有一个带有文本“禁用”的按钮。我希望在页面打开时将其禁用。当用户单击按钮时,我希望我的表单字段被启用,同时我希望我的按钮值被更改为“启用” 我有一个如何更改按钮值的示例,但如何同时启用/禁用它Javascript 启用/禁用表单元素并一次更改按钮的值?,javascript,jquery,jsf,jsf-2,primefaces,Javascript,Jquery,Jsf,Jsf 2,Primefaces,要求:有一个带有文本“禁用”的按钮。我希望在页面打开时将其禁用。当用户单击按钮时,我希望我的表单字段被启用,同时我希望我的按钮值被更改为“启用” 我有一个如何更改按钮值的示例,但如何同时启用/禁用它 <h:form> Please press the Button it. <h:commandButton id="cb1" value="#{ActionBean.buttonText}" immediate="true" actionListener="#{Ac
<h:form>
Please press the Button it.
<h:commandButton id="cb1" value="#{ActionBean.buttonText}"
immediate="true" actionListener="#{ActionBean.processMyAction}" />
</h:form>
下面是一个供您尝试的示例
<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>
你的要求不清楚。你想要一个按钮来禁用文本并同时禁用按钮吗?@Adarsh我想我在编辑时把这个搞糟了。仔细想想,它的意思是“当页面打开时,我想让它说‘disabled’”,即用户单击按钮,按钮被禁用,其值更改为“enabled”(至少这是唯一有意义的方式).@mabi在这种情况下,按钮显示为启用后将不再有任何用途,因为按钮本身将被禁用。这与他提供给我们的代码并不完全一致。@adarsh yes,例如,假设有一个编辑按钮,在单击编辑按钮时应首先禁用该按钮,我希望启用我的表单元素,单击按钮后,按钮值应更改为saveOk。这就是我的理解。页面上有一个按钮。加载页面时,您有1个按钮,其中文本启用和按钮启用。单击按钮时,“启用”按钮文本将更改为“禁用”,并且表单字段应可编辑。再次单击按钮时,按钮文本将更改为disable,表单元素将被禁用。method not found错误将出现164b321。changeButton()检查bean和jsf页面上的方法名称。当编辑按钮在那里时,我希望我的表单字段被禁用,这很好:谢谢你,实际上我对jpa&jsf应用程序还有一个疑问,你能帮我吗out@user3227175你可以为此发布一个新问题。我会尽力帮忙的。
<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;
}