Modal With Form
<!-- 触发按钮 -->
<button onclick="modal_form.showModal()" class="px-6 py-3 bg-blue-600 text-white font-medium rounded-lg shadow hover:bg-blue-700 transition-colors">
Open Form Modal
</button>
<!-- 带表单的模态框 -->
<dialog id="modal_form" class="modal rounded-lg shadow-xl">
<div class="modal-box w-11/12 max-w-md bg-white p-0 rounded-lg">
<!-- 标题区 -->
<div class="p-4 border-b border-gray-200">
<h3 class="text-lg font-bold text-gray-900">Contact Form</h3>
</div>
<!-- 带表单的内容区 -->
<div class="p-4">
<p class="text-gray-600 mb-4">
Please fill out this form to contact us. We'll respond as soon as possible.
</p>
<form id="contactForm" class="space-y-4">
<!-- 姓名字段 -->
<div>
<label for="name" class="block text-sm font-medium text-gray-700 mb-1">Full Name</label>
<input
type="text"
id="name"
name="name"
class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
placeholder="John Doe"
required
>
</div>
<!-- 邮箱字段 -->
<div>
<label for="email" class="block text-sm font-medium text-gray-700 mb-1">Email Address</label>
<input
type="email"
id="email"
name="email"
class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
placeholder="[email protected]"
required
>
</div>
<!-- 信息字段 -->
<div>
<label for="message" class="block text-sm font-medium text-gray-700 mb-1">Message</label>
<textarea
id="message"
name="message"
rows="4"
class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
placeholder="Your message here..."
required
></textarea>
</div>
</form>
</div>
<!-- 底部按钮区 -->
<div class="p-4 border-t border-gray-200 flex justify-end space-x-3">
<form method="dialog">
<button class="px-4 py-2 bg-gray-200 text-gray-700 font-medium rounded-lg hover:bg-gray-300 transition-colors">
Cancel
</button>
</form>
<button
type="submit"
form="contactForm"
onclick="submitForm()"
class="px-4 py-2 bg-blue-600 text-white font-medium rounded-lg shadow hover:bg-blue-700 transition-colors"
>
Submit
</button>
</div>
</div>
</dialog>
<style>
.modal {
max-height: 90vh;
}
.modal-box {
overflow-y: auto;
}
.modal-backdrop {
background-color: rgba(0, 0, 0, 0.3);
}
</style>
<script>
function submitForm() {
const form = document.getElementById('contactForm');
if (form.checkValidity()) {
// Here would go the code to submit the form data
alert('Form submitted successfully!');
document.getElementById('modal_form').close();
} else {
// Trigger browser's native form validation
form.reportValidity();
}
}
</script>
Copied to clipboard