用bootstrap做一个登陆注册页面代码(要有中文注释) 1.注册和登录可以切换 2.注册成功会跳转到登陆 3.注册失败会有提示:用户名已经存在 4.注册信息发送到controller,判断是否存在 5.可以让我复制代码
时间: 2024-06-06 13:11:32 浏览: 135
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>登陆注册页面</title>
<!-- 引入Bootstrap样式文件 -->
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<!-- 登陆和注册切换按钮 -->
<div class="text-center mb-3">
<button class="btn btn-primary" id="loginBtn">登陆</button>
<button class="btn btn-secondary" id="registerBtn">注册</button>
</div>
<!-- 登陆表单 -->
<form action="#" method="POST" id="loginForm">
<div class="form-group">
<label for="loginUsername">用户名:</label>
<input type="text" class="form-control" id="loginUsername" name="loginUsername">
</div>
<div class="form-group">
<label for="loginPassword">密码:</label>
<input type="password" class="form-control" id="loginPassword" name="loginPassword">
</div>
<button type="submit" class="btn btn-primary">登陆</button>
</form>
<!-- 注册表单 -->
<form action="#" method="POST" id="registerForm" style="display:none">
<div class="form-group">
<label for="registerUsername">用户名:</label>
<input type="text" class="form-control" id="registerUsername" name="registerUsername">
</div>
<div class="form-group">
<label for="registerPassword">密码:</label>
<input type="password" class="form-control" id="registerPassword" name="registerPassword">
</div>
<div class="form-group">
<label for="registerConfirmPassword">确认密码:</label>
<input type="password" class="form-control" id="registerConfirmPassword" name="registerConfirmPassword">
</div>
<button type="submit" class="btn btn-primary">注册</button>
</form>
<!-- 注册成功提示框 -->
<div class="alert alert-success mt-3" id="registerSuccess" style="display:none">
注册成功!请返回登陆。
</div>
<!-- 注册失败提示框 -->
<div class="alert alert-danger mt-3" id="registerFail" style="display:none">
用户名已经存在,请重新输入。
</div>
</div>
<!-- 引入jQuery和Bootstrap的JavaScript文件 -->
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
$(function () {
// 切换登陆和注册表单
$('#loginBtn').click(function () {
$('#loginForm').show();
$('#registerForm').hide();
$('#registerSuccess').hide();
$('#registerFail').hide();
$(this).removeClass('btn-secondary').addClass('btn-primary');
$('#registerBtn').removeClass('btn-primary').addClass('btn-secondary');
});
$('#registerBtn').click(function () {
$('#loginForm').hide();
$('#registerForm').show();
$('#registerSuccess').hide();
$('#registerFail').hide();
$(this).removeClass('btn-secondary').addClass('btn-primary');
$('#loginBtn').removeClass('btn-primary').addClass('btn-secondary');
});
// 注册表单提交
$('#registerForm').submit(function (e) {
e.preventDefault();
var username = $('#registerUsername').val();
var password = $('#registerPassword').val();
var confirmPassword = $('#registerConfirmPassword').val();
// 判断两次输入的密码是否一致
if (password !== confirmPassword) {
alert('两次输入的密码不一致!');
return;
}
// 发送注册请求
$.ajax({
url: '/register',
type: 'POST',
data: {username: username, password: password},
success: function (res) {
if (res.code === 0) {
// 注册成功,显示提示框并跳转到登陆
$('#registerSuccess').show();
setTimeout(function () {
$('#loginBtn').click();
}, 2000);
} else {
// 注册失败,显示提示框
$('#registerFail').show();
}
}
});
});
});
</script>
</body>
</html>
阅读全文