如何创建:多步骤表单

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

表单向导 - 多步骤表单:

Register:
Name:

Contact Info:

Birthday:

Login Info:

亲自试一试

第一步 - 添加 HTML:

<form id="regForm" action="">
<h1>Register:</h1>
<!-- 表单中的每个步骤都有一个“标签页”: -->
<div class="tab">Name:</div>
  <p><input placeholder="First name..." oninput="this.className = ''"></p>
  <p><input placeholder="Last name..." oninput="this.className = ''"></p>
</div>
<div class="tab">Contact Info:</div>
  <p><input placeholder="E-mail..." oninput="this.className = ''"></p>
  <p><input placeholder="Phone..." oninput="this.className = ''"></p>
</div>
<div class="tab">Birthday:</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">Login Info:</div>
  <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>

ဒုတိယအစုစုပေါင်း - ပုံစံ ထပ်ပေါင်းရန်:

ပုံစံ ဖော်ပြရန် ပုံစံအရင်း:

/* ပုံစံ ဖော်ပြရန် ပုံစံကုဒ်: */
#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;
}

တတိယ အပိုင်း - ဂျာမန် ကို ထပ်ထည့်ပါ:

var currentTab = 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";
  }
    document.getElementById("prevBtn").style.display = "inline";
  }
  if (n == (x.length - 1)) {
    document.getElementById("nextBtn").innerHTML = "Submit";
  }
    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";
  // ယခု အချက်ပုံ အချက်အရာများ တိုးပွားစေရမည်
  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";
      // ယခု အခြေအနေ အချက် အချက်ပြု ပါ
      valid = false;
    }
  }
  // အခြေအနေ အချက် true ဖြစ်လျှင် အပိုင်းကို ပြီးပြတ်နှင့် အခြေအနေ အချက် အချက်ပြုပါ
  if (valid) {
    document.getElementsByClassName("step")[currentTab].className += " finish";
  }
  return valid; // အခြေအနေ အချက် တင်ပါ
}
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";
}

亲自试一试