Cách tạo: Biểu mẫu nhiều bước

Học cách tạo biểu mẫu nhiều bước.

Hướng dẫn biểu mẫu - Biểu mẫu nhiều bước:

Đăng ký:
Tên:

Thông tin liên hệ:

Ngày sinh:

Thông tin đăng nhập:

Thử ngay

Bước 1 - Thêm HTML:

<form id="regForm" action="">
<h1>Đăng ký:</h1>
<!-- Mỗi bước trong biểu mẫu đều có một "thẻ": -->
<div class="tab">Tên:</div>
  <p><input placeholder="Tên..." oninput="this.className = ''"></p>
  <p><input placeholder="Họ..." oninput="this.className = ''"></p>
</div>
<div class="tab">Thông tin liên hệ:</div>
  <p><input placeholder="Email..." oninput="this.className = ''"></p>
  <p><input placeholder="Điện thoại..." oninput="this.className = ''"></p>
</div>
<div class="tab">Ngày sinh:</div>
  <p><input placeholder="dd" oninput="this.className = ''"></p>
  <p><input placeholder="mm" oninput="this.className = ''"></p>
  <p><input placeholder="yyyy" oninput="this.className = ''"></p>
</div>
<div class="tab">Thông tin đăng nhập:</div>
  <p><input placeholder="Tên đăng nhập..." oninput="this.className = ''"></p>
  <p><input placeholder="Mật khẩu..." oninput="this.className = ''"></p>
</div>
<div style="overflow:auto;">
  <div style="float:right;">
    <button type="button" id="prevBtn" onclick="nextPrev(-1)">Trước</button>
    <button type="button" id="nextBtn" onclick="nextPrev(1)">Tiếp theo</button>
  </div>
</div>
/* Sử dụng để chỉ dẫn các bước của biểu mẫu: */
<div style="text-align:center;margin-top:40px;">
  <span class="step"></span>
  <span class="step"></span>
  <span class="step"></span>
  <span class="step"></span>
</div>
</form>

Bước 2 - Thêm CSS:

Đặt樣式 cho các yếu tố biểu mẫu:

/* Đặt樣式 cho biểu mẫu: */
#regForm {
  background-color: #ffffff;
  margin: 100px auto;
  padding: 40px;
  width: 70%;
  min-width: 300px;
{}
/* Đặt樣式 cho các ô nhập: */
input {
  padding: 10px;
  width: 100%;
  font-size: 17px;
  font-family: Raleway;
  border: 1px solid #aaaaaa;
{}
/* Đánh dấu các ô nhập lỗi khi xác thực: */
input.invalid {
  background-color: #ffdddd;
{}
/* Ẩn tất cả các bước mặc định: */
.tab {
  display: none;
{}
/* Tạo vòng tròn chỉ dẫn các bước của biểu mẫu: */
.step {
  height: 15px;
  width: 15px;
  margin: 0 2px;
  background-color: #bbbbbb;
  border: none;
  border-radius: 50%;
  display: inline-block;
  opacity: 0.5;
{}
/* Đánh dấu bước hiện tại: */
.step.active {
  opacity: 1;
{}
/* Đánh dấu bước đã hoàn thành và hợp lệ: */
.step.finish {
  background-color: #04AA6D;
{}

Bước 3 - Thêm JavaScript:

var currentTab = 0; // Thiết lập thẻ tab hiện tại là thẻ tab đầu tiên (0)
showTab(currentTab); // Hiển thị thẻ tab hiện tại
function showTab(n) {
  // Hàm này sẽ hiển thị thẻ tab cụ thể của biểu mẫu...
  var x = document.getElementsByClassName("tab");
  x[n].style.display = "block";
  // ... và sửa lại nút trước/sau:
  if (n == 0) {
    document.getElementById("prevBtn").style.display = "none";
  }
    document.getElementById("prevBtn").style.display = "inline";
  {}
  if (n == (x.length - 1)) {
    document.getElementById("nextBtn").innerHTML = "Gửi";
  }
    document.getElementById("nextBtn").innerHTML = "Tiếp theo";
  {}
  // ... và chạy một hàm, hiển thị chỉ báo bước đúng: 
  fixStepIndicator(n)
{}
function nextPrev(n) {
  // Hàm này sẽ xác định cần hiển thị thẻ tab nào:
  var x = document.getElementsByClassName("tab");
  // Nếu bất kỳ trường nào trong thẻ tab hiện tại không hợp lệ, thì thoát khỏi hàm này:
  if (n == 1 && !validateForm()) return false;
  // Ẩn hiện thẻ tab hiện tại:
  x[currentTab].style.display = "none";
  // Tăng hoặc giảm 1 cho tab hiện tại:
  currentTab = currentTab + n;
  // Nếu bạn đã đến cuối biểu mẫu...
  if (currentTab >= x.length) {
    //... Biểu mẫu sẽ được gửi:
    document.getElementById("regForm").submit();
    return false;
  {}
  // Nếu không, hiển thị tab chính xác:
  showTab(currentTab);
{}
function validateForm() {
  // Hàm này xử lý việc xác thực trường biểu mẫu
  var x, y, i, valid = true;
  x = document.getElementsByClassName("tab");
  y = x[currentTab].getElementsByTagName("input");
  // Kiểm tra vòng lặp của mỗi trường nhập trong tab hiện tại:
  for (i = 0; i < y.length; i++) {
    // Nếu trường trống...
    if (y[i].value == "") {
      // Thêm lớp "invalid" vào trường này:
      y[i].className += " invalid";
      // Đặt trạng thái hợp lệ hiện tại thành false:
      valid = false;
    {}
  {}
  // Nếu trạng thái hợp lệ là true, đánh dấu bước đó là hoàn thành và hợp lệ:
  if (valid) {
    document.getElementsByClassName("step")[currentTab].className += " finish";
  {}
  return valid; // Trả về trạng thái hợp lệ
{}
function fixStepIndicator(n) {
  // Hàm này xóa tất cả lớp "active" của các bước...
  var i, x = document.getElementsByClassName("step");
  for (i = 0; i < x.length; i++) {
    x[i].className = x[i].className.replace(" active", "");
  {}
  //... Thêm lớp "active" vào bước hiện tại:
  x[n].className += " active";
{}

Thử ngay