我正在嘗試使用JSF創建一個使用用戶名(顯示),角色(隱藏)和密碼(隱藏)的單獨表單。如何使用JavaScript顯示/隱藏HTML元素
首先,用戶輸入他/她的用戶名,然後點擊一個「驗證」按鈕。如果用戶名存在,則頁面必須顯示用戶名角色,密碼輸入的角色<h:SelectOneMenu>
,並且還隱藏「驗證」按鈕。
中的作用和密碼輸入默認情況下通過CSS隱藏:
<body>
<div class="container">
<h:form styleClass="form-signin">
<h2 class="form-signin-heading">SISE<br><small>Inicio de Sesión</small></h2>
<label for="inputEmail" class="sr-only">Nombre de Usuario</label>
<input type="text" id="inputEmail" class="form-control" placeholder="Nombre de usuario" required autofocus>
<br>
<h:commandButton styleClass="btn btn-lg btn-primary btn-block" value="Validar" action="#{beanInicioSesion.buscarUsuarioDB}" />
<h:selectOneMenu styleClass="form-control hide">
<f:selectItem itemLabel="Something" itemValue="Something"></f:selectItem>
</h:selectOneMenu>
<label for="inputPassword" class="sr-only">Contraseña</label>
<input type="password" id="inputPassword" class="form-control hide" placeholder="Contraseña" required>
<br>
<h:commandButton styleClass="btn btn-lg btn-primary btn-block hide" value="Iniciar sesión" />
</h:form>
</div>
</body>
以下是我在我的管理用戶的登入豆:
public String buscarUsuarioDB() throws SISE_Exceptions, SQLException {
int usuarioValidado = 0; //validatedUser
UsuarioDB usrDB = new UsuarioDB();
try {
usuarioValidado = usrDB.consultarUsuario(this.getNombreUsuario());
if (usuarioValidado == 0) {
setMensaje("Usuario Invalido");
} else {
setMensaje("Usuario Valido");
//Valid user, then show/display the <h:selectOneMenu> and password input.
}
} catch (Exception e) {
// TODO: Add catch code
e.printStackTrace();
}
return "";
}
所以問題是,如何從我的buscarUsuarioDB()方法中選擇HTML元素,以便將它們設置爲在JSP中顯示/隱藏?
你需要一些Ajax做這個username.exists-check live – Rick