작성 방법: 다단계 폼
- 이전 페이지 암호 보이기/숨기기 전환
- 다음 페이지 자동 완성
학습하려면 다단계 폼을 만드는 방법을 배웁니다.
表单向导 - 多步骤表单:
第一步 - 添加 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>
第二步 - 添加 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); // 현재 탭 표시 // 이 함수는 지정된 탭을 표시합니다: x[n].style.display = "block"; var x = document.getElementsByClassName("tab"); // ... 이전/다음 버튼을 수정합니다: 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"; 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; // 유효 상태를 반환 } 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"; }
- 이전 페이지 암호 보이기/숨기기 전환
- 다음 페이지 자동 완성