用bootstrap做一个登陆注册页面代码(要有中文注释) 1.注册和登录可以切换 2.注册成功会跳转到登陆 3.注册失败会有提示:用户名已经存在 4.注册信息发送到controller,判断是否存在
时间: 2024-05-29 11:12:45 浏览: 72
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陆和注册</title>
<!--引入bootstrap-->
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css">
</head>
<body>
<!--导航栏-->
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">登陆和注册</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" id="login-link" href="#">登陆</a>
</li>
<li class="nav-item">
<a class="nav-link" id="register-link" href="#">注册</a>
</li>
</ul>
</div>
</nav>
<!--登陆表单-->
<div class="container" id="login-form">
<h3>欢迎回来!</h3>
<form>
<div class="form-group">
<label for="login-username">用户名</label>
<input type="text" class="form-control" id="login-username" placeholder="请输入用户名">
</div>
<div class="form-group">
<label for="login-password">密码</label>
<input type="password" class="form-control" id="login-password" placeholder="请输入密码">
</div>
<button type="submit" class="btn btn-primary">登陆</button>
</form>
</div>
<!--注册表单-->
<div class="container" id="register-form" style="display: none;">
<h3>欢迎加入我们!</h3>
<form>
<div class="form-group">
<label for="register-username">用户名</label>
<input type="text" class="form-control" id="register-username" placeholder="请输入用户名">
<span id="username-exist" style="color: red; display: none;">用户名已经存在</span>
</div>
<div class="form-group">
<label for="register-password">密码</label>
<input type="password" class="form-control" id="register-password" placeholder="请输入密码">
</div>
<button type="submit" class="btn btn-primary">注册</button>
</form>
</div>
<!--引入jquery和bootstrap的js文件-->
<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.0/js/bootstrap.min.js"></script>
<script>
$(function () {
//切换到登陆表单
$('#login-link').click(function () {
$('#login-form').show();
$('#register-form').hide();
return false;
});
//切换到注册表单
$('#register-link').click(function () {
$('#register-form').show();
$('#login-form').hide();
return false;
});
//注册表单提交
$('#register-form form').submit(function () {
//获取用户名和密码
var username = $('#register-username').val();
var password = $('#register-password').val();
//调用controller判断用户名是否存在
$.ajax({
url: '/check-username',
method: 'POST',
data: {
username: username
},
success: function (data) {
if (data.exist) {
//用户名已经存在,显示提示信息
$('#username-exist').show();
} else {
//用户名不存在,提交注册信息
$.ajax({
url: '/register',
method: 'POST',
data: {
username: username,
password: password
},
success: function () {
alert('注册成功!请登陆');
//切换到登陆表单
$('#login-form').show();
$('#register-form').hide();
}
});
}
}
});
return false;
});
});
</script>
</body>
</html>
阅读全文