创建有效的多步骤表单以获得更好的用户体验
创作时间:
作者:
@小白创作中心
创建有效的多步骤表单以获得更好的用户体验
引用
1
来源
1.
https://www.jiangweishan.com/article/fonmbiaodan23523589.html
多步骤表单是Web开发中常见的一种表单类型,它将复杂的表单数据输入过程分解为多个步骤,从而提高用户体验。本文将详细介绍如何创建一个功能完善的多步骤表单,包括HTML结构、CSS样式、JavaScript交互以及用户体验优化等方面的内容。
表单结构设计
在创建多步骤表单时,首先需要规划各个步骤的逻辑顺序和内容。对于一个工作申请表单,我们可以将其分为以下几个步骤:
- 个人信息:收集申请人的姓名、电子邮件和电话号码。
- 工作经验:收集申请人的最近的公司、职位和工作年限。
- 技能和资格:申请人列出他们的技能并选择他们的最高学位。
- 审核并提交:提供一个机会让申请人在提交前审核所有信息。
根据上述步骤,我们可以设计表单的HTML结构:
<form id="jobApplicationForm">
<!-- Step 1: Personal Information -->
<fieldset class="step" id="step-1">
<legend id="step1Label">Step 1: Personal Information</legend>
<label for="name">Full Name</label>
<input type="text" id="name" name="name" required />
<label for="email">Email Address</label>
<input type="email" id="email" name="email" required />
<label for="phone">Phone Number</label>
<input type="tel" id="phone" name="phone" required />
</fieldset>
<!-- Step 2: Work Experience -->
<fieldset class="step" id="step-2" hidden>
<legend id="step2Label">Step 2: Work Experience</legend>
<label for="company">Most Recent Company</label>
<input type="text" id="company" name="company" required />
<label for="jobTitle">Job Title</label>
<input type="text" id="jobTitle" name="jobTitle" required />
<label for="yearsExperience">Years of Experience</label>
<input type="number" id="yearsExperience" name="yearsExperience" min="0" required />
</fieldset>
<!-- Step 3: Skills & Qualifications -->
<fieldset class="step" id="step-3" hidden>
<legend id="step3Label">Step 3: Skills & Qualifications</legend>
<label for="skills">Skill(s)</label>
<textarea id="skills" name="skills" rows="4" required></textarea>
<label for="highestDegree">Degree Obtained (Highest)</label>
<select id="highestDegree" name="highestDegree" required>
<option value="">Select Degree</option>
<option value="highschool">High School Diploma</option>
<option value="bachelor">Bachelor's Degree</option>
<option value="master">Master's Degree</option>
<option value="phd">Ph.D.</option>
</select>
</fieldset>
<!-- Step 4: Review & Submit -->
<fieldset class="step" id="step-4" hidden>
<legend id="step4Label">Step 4: Review & Submit</legend>
<p>Review your information before submitting the application.</p>
<button type="submit">Submit Application</button>
</fieldset>
</form>
样式设计
为了保持简洁的外观,我们可以使用Simple.css样式框架,并添加一些自定义样式:
body {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
main {
padding: 0 30px;
}
h1 {
font-size: 1.8rem;
text-align: center;
}
.stepper {
display: flex;
justify-content: flex-end;
padding-right: 10px;
}
form {
box-shadow: 0px 0px 6px 2px rgba(0, 0, 0, 0.2);
padding: 12px;
}
input,
textarea,
select {
outline: none;
}
input:valid,
textarea:valid,
select:valid,
input:focus:valid,
textarea:focus:valid,
select:focus:valid {
border-color: green;
}
input:focus:invalid,
textarea:focus:invalid,
select:focus:invalid {
border: 1px solid red;
}
表单导航和验证
为了确保用户在每一步都能获得及时的反馈,我们需要在进入下一步之前验证当前步骤的输入。我们可以通过JavaScript实现这一功能:
<script>
const steps = document.querySelectorAll(".step");
let currentStep = 1;
let errorMsgs = [];
const errorMessagesDiv = document.getElementById("errorMessages");
const currentStepDiv = document.querySelector(".currentStep");
function nextStep() {
errorMsgs = [];
errorMessagesDiv.innerText = "";
switch (currentStep) {
case 1:
addValidationErrors(name, email, phone);
validateStep(errorMsgs);
break;
case 2:
addValidationErrors(company, jobTitle, yearsExperience);
validateStep(errorMsgs);
break;
case 3:
addValidationErrors(skills, highestDegree);
validateStep(errorMsgs);
break;
}
}
function addValidationErrors(fieldOne, fieldTwo, fieldThree = undefined) {
if (!fieldOne.checkValidity()) {
const label = document.querySelector(`label[for="${fieldOne.id}"]`);
errorMsgs.push(`Please Enter A Valid ${label.textContent}`);
}
if (!fieldTwo.checkValidity()) {
const label = document.querySelector(`label[for="${fieldTwo.id}"]`);
errorMsgs.push(`Please Enter A Valid ${label.textContent}`);
}
if (fieldThree && !fieldThree.checkValidity()) {
const label = document.querySelector(`label[for="${fieldThree.id}"]`);
errorMsgs.push(`Please Enter A Valid ${label.textContent}`);
}
if (errorMsgs.length > 0) {
errorMessagesDiv.innerText = errorMsgs.join("\n");
}
}
function validateStep(errorMsgs) {
if (errorMsgs.length === 0) {
showStep(currentStep + 1);
}
}
function showStep(step) {
steps.forEach((el, index) => {
el.hidden = index + 1 !== step;
});
currentStep = step;
currentStepDiv.innerText = currentStep;
localStorage.setItem("storedStep", currentStep);
}
function previousStep() {
errorMessagesDiv.innerText = "";
showStep(currentStep - 1);
}
// Save data on each input event
form.addEventListener("input", () => {
const formData = {
name: document.getElementById("name").value,
email: document.getElementById("email").value,
phone: document.getElementById("phone").value,
company: document.getElementById("company").value,
jobTitle: document.getElementById("jobTitle").value,
yearsExperience: document.getElementById("yearsExperience").value,
skills: document.getElementById("skills").value,
highestDegree: document.getElementById("highestDegree").value,
};
localStorage.setItem("formData", JSON.stringify(formData));
});
// Retrieve data on DOMContentLoaded
window.addEventListener("DOMContentLoaded", () => {
const savedData = JSON.parse(localStorage.getItem("formData"));
if (savedData) {
document.getElementById("name").value = savedData.name || "";
document.getElementById("email").value = savedData.email || "";
document.getElementById("phone").value = savedData.phone || "";
document.getElementById("company").value = savedData.company || "";
document.getElementById("jobTitle").value = savedData.jobTitle || "";
document.getElementById("yearsExperience").value = savedData.yearsExperience || "";
document.getElementById("skills").value = savedData.skills || "";
document.getElementById("highestDegree").value = savedData.highestDegree || "";
}
const storedStep = localStorage.getItem("storedStep");
if (storedStep) {
const storedStepInt = parseInt(storedStep);
steps.forEach((el, index) => {
el.hidden = index + 1 !== storedStepInt;
});
currentStep = storedStepInt;
currentStepDiv.innerText = currentStep;
}
});
// Clear data on form submit
form.addEventListener("submit", () => {
localStorage.removeItem("formData");
localStorage.removeItem("storedStep");
});
</script>
用户体验优化
为了进一步提升用户体验,我们可以添加一个步进器(stepper)来显示当前步骤:
<form id="jobApplicationForm">
<div class="stepper">
<span><span class="currentStep">1</span>/4</span>
</div>
<!-- ... -->
</form>
通过以上步骤,我们可以创建一个功能完善且用户体验良好的多步骤表单。这种表单结构不仅能够帮助用户更清晰地完成复杂的表单填写过程,还能通过即时验证和数据存储等功能提升整体的使用体验。
热门推荐
运算放大器参数计算与设计要点详解
感冒如何不被传染
影响力翻译:解构语言背后的深层意义
家装水电工需要电工证吗?详解从业资格与安全规范
离婚后多久可以再婚
长时间戴蒸汽眼罩可能导致视力下降
蒸汽眼罩真的可以缓解眼疲劳吗?
畅游无忧:如何有效预防游泳引发的中耳炎?
安海镇赴福州考察:深化合作,共谋发展新篇章
杨笠代言风波:京东股价逆流而上?
为什么杨笠被骂这么惨,还不断有品牌请她做代言?
100千瓦设备应选用多大规格的电缆?专业计算与选型指南
主板内存怎么选择?
吊钟海棠养护与种植技巧 | Fuchsia hybrida盆栽指南
爆炒龙虾怎么炒才好吃?这个做法真好吃!
永久牌山地车变速器调整方法,山地车变速器调整
如何测试翻译软件准确性:从基础对比到专业评估
特稿:中国电动车市场快步发展但销量已放缓 潜力仍有待释放
深海鱼油和鱼肝油哪个对眼睛更好?
如何进行反向投资操作?这种操作有哪些技巧?
PSP钢塑复合管:建筑领域的新型管道材料
伤口护理全攻略:快速愈合的秘诀与注意事项
一段老化的电线线路会引发什么?
失火罪的构成要件以及怎么判刑
电子仿真 SPICE 课程:初始条件和 .IC 指令
毒驾行为如何构成危险驾驶罪:法律解析与实务探讨
毒驾行为如何构成危险驾驶罪:法律解析与实务探讨
洗牙喷砂好还是不喷砂好?别争了!洗牙有没有必要喷砂是牙齿决定的!
牙龈发炎可以吃罗红霉素吗
牙龈肿痛怎么办?从中医证候、经络与中药理解!