document.addEventListener('DOMContentLoaded', function() { const registerTeacherForm = document.getElementById('register-teacher-form'); registerTeacherForm.addEventListener('submit', function(event) { event.preventDefault(); // 阻止表单默认提交行为 const username = document.getElementById('teacher-username').value; const email = document.getElementById('teacher-email').value; const password = document.getElementById('teacher-password').value; const confirmPassword = document.getElementById('teacher-confirm-password').value; // 检查密码和确认密码是否一致 if (password !== confirmPassword) { alert('密码和确认密码不一致'); return; } // 使用 Fetch API 发送注册请求 fetch('/register_teacher_post', { // 教师注册路由 method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRFToken': '{{ csrf_token() }}' // 如果使用 CSRF 保护 }, body: JSON.stringify({ username, email, password }) // 发送数据 }) .then(response => response.json()) .then(data => { if (data.success) { alert('教师注册成功'); window.location.href = '/login'; // 注册成功后跳转到登录页面 } else { alert('注册失败: ' + data.message); } }) .catch(error => { console.error('Error:', error); alert('注册失败'); }); }); });