如何创建:多步骤表单

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

表单向导 - 多步骤表单:

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; // বর্তমান ট্যাবটি প্রথম ট্যাবটি (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";
  // Increase or decrease the current tab by 1:
  currentTab = currentTab + n;
  // If you have reached the end of the table...:
  if (currentTab >= x.length) {
    //... The form will be submitted:
    document.getElementById("regForm").submit();
    return false;
  }
  // Otherwise, display the correct tab:
  showTab(currentTab);
}
function validateForm() {
  // This function handles form field validation
  var x, y, i, valid = true;
  x = document.getElementsByClassName("tab");
  y = x[currentTab].getElementsByTagName("input");
  // Loop through each input field in the current tab:
  for (i = 0; i < y.length; i++) {
    // If the field is empty...
    if (y[i].value == "") {
      // Add the "invalid" class to the field:
      y[i].className += " invalid";
      // And set the current valid status to false:
      valid = false;
    }
  }
  // If the valid status is true, mark the step as completed and valid:
  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";
}

亲自试一试