设计一个登录界面 html 提交到远程服务器 接收成功后跳转到其他页面
时间: 2023-08-08 08:04:56 浏览: 50
以下是一个简单的登录界面的 HTML 代码,包含用户名、密码和登录按钮,并且在登录成功后跳转到其他页面:
```html
<!DOCTYPE html>
<html>
<head>
<title>登录</title>
<meta charset="utf-8">
<style>
body {
background-color: #f5f5f5;
font-family: Arial, sans-serif;
}
h1 {
text-align: center;
margin-top: 50px;
}
form {
width: 300px;
margin: 0 auto;
background-color: #fff;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 5px;
}
label {
display: block;
margin-bottom: 10px;
}
input[type="text"], input[type="password"] {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 3px;
font-size: 16px;
margin-bottom: 20px;
box-sizing: border-box;
}
input[type="submit"] {
background-color: #4CAF50;
color: #fff;
border: none;
padding: 10px 20px;
font-size: 16px;
border-radius: 3px;
cursor: pointer;
transition: background-color 0.3s ease;
}
input[type="submit"]:hover {
background-color: #3e8e41;
}
</style>
</head>
<body>
<h1>登录</h1>
<form id="login-form" action="http://example.com/login" method="post">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required>
<input type="submit" value="登录">
</form>
<script>
// 获取表单元素和提交地址
const form = document.getElementById('login-form');
const action = form.action;
// 监听表单提交事件
form.addEventListener('submit', function(event) {
event.preventDefault(); // 阻止表单默认提交行为
// 发送 AJAX 请求
const xhr = new XMLHttpRequest();
xhr.open('POST', action);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
alert('登录成功!');
window.location.href = 'http://example.com/other-page'; // 跳转到其他页面
} else {
alert('登录失败!');
}
};
xhr.send(new FormData(form));
});
</script>
</body>
</html>
```
这个登录界面使用了 HTML、CSS 和 JavaScript,采用了简洁的扁平化设计风格,使用了表单元素来实现输入框和登录按钮,同时添加了一些简单的样式来美化界面。在表单提交时,通过 AJAX 发送请求到远程服务器,如果服务器返回状态码为 200,则表示登录成功,此时通过 `window.location.href` 跳转到其他页面。你可以根据需要进行修改和完善。注意,这里的 `action` 属性需要设置为远程服务器的登录接口地址。