如何创建:多步骤表单

学习如何创建一个多步骤的表单。

表单向导 - 多步骤表单:

Register:
Name:

Contact Info:

Birthday:

Login Info:

亲自试一试

第一步 - 添加 HTML:

<form id="regForm" action="">

<h1>Register:</h1>

<!-- 表单中的每个步骤都有一个“标签页”: -->
<div class="tab">Name:
  <p><input placeholder="First name..." oninput="this.className = ''"></p>
  <p><input placeholder="Last name..." oninput="this.className = ''"></p>
</div>

<div class="tab">Contact Info:
  <p><input placeholder="E-mail..." oninput="this.className = ''"></p>
  <p><input placeholder="Phone..." oninput="this.className = ''"></p>
</div>

<div class="tab">Birthday:
  <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">Login Info:
  <p><input placeholder="Username..." oninput="this.className = ''"></p>
  <p><input placeholder="Password..." oninput="this.className = ''"></p>
</div>

<div style="overflow:auto;">
  <div style="float:right;">
    <button type="button" id="prevBtn" onclick="nextPrev(-1)">Previous</button>
    <button type="button" id="nextBtn" onclick="nextPrev(1)">Next</button>
  </div>
</div>

<!-- 用于指示表单步骤的圆圈: -->
<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>

第二步 - 添加 CSS:

设置表单元素的样式:

/* 为表单设置样式 */
#regForm {
  background-color: #ffffff;
  margin: 100px auto;
  padding: 40px;
  width: 70%;
  min-width: 300px;
}

/* 为输入框设置样式 */
input {
  padding: 10px;
  width: 100%;
  font-size: 17px;
  font-family: Raleway;
  border: 1px solid #aaaaaa;
}

/* 标记验证时出错的输入框: */
input.invalid {
  background-color: #ffdddd;
}

/* 默认隐藏所有步骤: */
.tab {
  display: none;
}

/* 创建指示表单步骤的圆圈: */
.step {
  height: 15px;
  width: 15px;
  margin: 0 2px;
  background-color: #bbbbbb;
  border: none;
  border-radius: 50%;
  display: inline-block;
  opacity: 0.5;
}

/* 标记当前步骤: */
.step.active {
  opacity: 1;
}

/* 标记已完成且有效的步骤: */
.step.finish {
  background-color: #04AA6D;
}

第三步 - 添加 JavaScript:

var currentTab = 0; // 当前标签页设置为第一个标签页 (0)
showTab(currentTab); // 显示当前标签页

function showTab(n) {
  // 此函数将显示表单的指定标签页 ...
  var x = document.getElementsByClassName("tab");
  x[n].style.display = "block";
  // ... 并修复上一个/下一个按钮:
  if (n == 0) {
    document.getElementById("prevBtn").style.display = "none";
  } else {
    document.getElementById("prevBtn").style.display = "inline";
  }
  if (n == (x.length - 1)) {
    document.getElementById("nextBtn").innerHTML = "Submit";
  } else {
    document.getElementById("nextBtn").innerHTML = "Next";
  }
  // ... 并运行一个函数,显示正确的步骤指示器: 
  fixStepIndicator(n)
}

function nextPrev(n) {
  // 此函数将确定要显示哪个标签页:
  var x = document.getElementsByClassName("tab");
  // 如果当前标签页中的任何字段无效,则退出该函数:
  if (n == 1 && !validateForm()) return false;
  // 隐藏当前标签页:
  x[currentTab].style.display = "none";
  // 将当前标签页增加或减少 1:
  currentTab = currentTab + n;
  // 如果您已到达表格末尾...:
  if (currentTab >= x.length) {
    //...表单将被提交:
    document.getElementById("regForm").submit();
    return false;
  }
  // 否则,显示正确的标签页:
  showTab(currentTab);
}

function validateForm() {
  // 该函数处理表单字段的验证
  var x, y, i, valid = true;
  x = document.getElementsByClassName("tab");
  y = x[currentTab].getElementsByTagName("input");
  // 检查当前标签页中每个输入字段的循环:
  for (i = 0; i < y.length; i++) {
    // 如果字段为空...
    if (y[i].value == "") {
      // 将 "invalid" 类添加到该字段:
      y[i].className += " invalid";
      // 并将当前有效状态设置为 false:
      valid = false;
    }
  }
  // 如果有效状态为 true,则将该步骤标记为已完成且有效:
  if (valid) {
    document.getElementsByClassName("step")[currentTab].className += " finish";
  }
  return valid; // return the valid status
}

function fixStepIndicator(n) {
  // 此函数删除所有步骤的 "active" 类...
  var i, x = document.getElementsByClassName("step");
  for (i = 0; i < x.length; i++) {
    x[i].className = x[i].className.replace(" active", "");
  }
  //... 并将 "active" 类添加到当前步骤:
  x[n].className += " active";
}

亲自试一试