以编程方式禁用按钮

时间:2017-06-05 19:42:40

标签: javascript jsf primefaces

我想禁用我的注册表单的提交按钮如果数据库中存在用户名我试过多个解决方案但是没有人工作

我的xhtml

<h:outputLabel for="#{userBean.username}" value="UserName" />
<p:inputText styleClass="input" id="username" value="#{userBean.username}" label="UserName " required="true"
    requiredMessage="Username is required"
    validatorMessage="UserName should be of maximum length 20 chars">
    <f:validateLength maximum="20"></f:validateLength>
    <p:ajax  event="keyup" listener="#{userBean.searchByUsername()}" update="usernamegrowl,signupButton" /> 
                </p:inputText>
<p:message  id="usernamemessage" for="username"></p:message>
<p:commandButton id="signupButton" value="SignUp"  action="#{userBean.persistUser()}"
                ajax="false" disabled="#{userBean.message}"></p:commandButton>

我的方法searchByUsername

public void searchByUsername() {
    User user = userService.findUserByUserName(getUsername());
    if (user != null) {
        FacesContext.getCurrentInstance().addMessage("usernamegrowl",
                new FacesMessage(FacesMessage.SEVERITY_ERROR, "username is alredy used", ""));
        RequestContext.getCurrentInstance().execute("document.getElementById('signupform:signupButton').setAttribute('disabled', 'true');");

        setMessage(false);
    }
    else
        setMessage(true);
    RequestContext.getCurrentInstance().execute("document.getElementById('signupform:signupButton').setAttribute('disabled', 'false');");

}

1 个答案:

答案 0 :(得分:0)

update以空格分隔。

有关处理/更新和执行/渲染的详细信息,请阅读此answer