<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>登录功能示例</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/mui@5.7.2/dist/css/mui.min.css">
<script src="https://cdn.jsdelivr.net/npm/mui@5.7.2/dist/js/mui.min.js"></script>
</head>
<body>
<div class="mui-container">
<h1>登录功能示例</h1>
<form id="loginForm">
<div class="mui-textfield mui-textfield--float-label">
<input type="text" id="username" required>
<label for="username">用户名</label>
</div>
<div class="mui-textfield mui-textfield--float-label">
<input type="password" id="password" required>
<label for="password">密码</label>
</div>
<button type="submit" class="mui-btn mui-btn--raised mui-btn--primary">登录</button>
</form>
</div>
<script>
document.getElementById('loginForm').addEventListener('submit', function(event) {
event.preventDefault();
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;
// 使用ajax实现登录功能
var xhr = new XMLHttpRequest();
xhr.open('POST', 'your_login_api_url', true);
xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
if (response.success) {
alert('登录成功');
// 跳转到其他页面
window.location.href = 'your_next_page_url';
} else {
alert('登录失败,请检查用户名和密码');
}
}
};
xhr.send(JSON.stringify({
username: username, password: password}));
});
</script>
</body>
</html>
请将上述代码中的your_login_api_url替换为实际的登录接口地址,将your_next_page_url替换为登录成功后需要跳转的页面地址。