国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目錄
>我們的工作申請(qǐng)表有四個(gè)部分,其中最後一個(gè)部分是摘要視圖,我們?cè)谔峤恢跋蛴脩粽故玖怂写鸢?。為此,我們將HTML分為四個(gè)部分,每個(gè)部分都用ID識(shí)別,並在頁(yè)面底部添加導(dǎo)航。我將在下一部分給您基線HTML。
>讓我們從創(chuàng)建一個(gè)文件夾以保存我們的頁(yè)面。然後,創(chuàng)建一個(gè)index.html文件,然後將以下內(nèi)容粘貼到其中:
>在多步格式中,在提交之前,在結(jié)尾向用戶摘要的所有答案摘要並為他們提供在必要時(shí)編輯答案的選項(xiàng)是有價(jià)值的。該人在不向後導(dǎo)航的情況下看不到前面的步驟,因此在最後一步中顯示摘要會(huì)提供保證和糾正任何錯(cuò)誤的機(jī)會(huì)。
>可訪問(wèn)性提示
參考
首頁(yè) web前端 css教學(xué) 如何使用香草JavaScript和CSS創(chuàng)建多步驟形式

如何使用香草JavaScript和CSS創(chuàng)建多步驟形式

Mar 07, 2025 pm 04:55 PM

How to Create Multi-Step Forms With Vanilla JavaScript and CSS

當(dāng)您的表格較大並且具有許多控件時(shí),

多步驟形式是一個(gè)不錯(cuò)的選擇。沒(méi)有人願(yuàn)意在移動(dòng)設(shè)備上滾動(dòng)瀏覽超長(zhǎng)形式。通過(guò)按屏幕為基礎(chǔ)對(duì)控件進(jìn)行分組,我們可以改善填寫長(zhǎng)長(zhǎng)的複雜形式的經(jīng)驗(yàn)。

>但是您上次開(kāi)發(fā)多步形式是什麼時(shí)候?這對(duì)您來(lái)說(shuō)聽(tīng)起來(lái)很有趣嗎?有太多的思考,需要管理的許多動(dòng)人的作品,以至於我不會(huì)怪您求助於表單庫(kù),甚至是某種為您處理所有這些形式的小部件。

>

但是,手工做可以是一種很好的練習(xí),也是拋光基礎(chǔ)知識(shí)的好方法。我將向您展示如何構(gòu)建我的第一個(gè)多步驟形式,希望您不僅可以看到它的接近程度,而且甚至可以發(fā)現(xiàn)使我的工作變得更好的領(lǐng)域。

>

我們將一起瀏覽結(jié)構(gòu)。我們將構(gòu)建一個(gè)工作應(yīng)用程序,我認(rèn)為我們中的許多人都可以與最近幾天有關(guān)。我將首先為基線HTML,CSS和JavaScript腳手架,然後我們將研究以訪問(wèn)性和驗(yàn)證的考慮。

>如果您想沿途參考,我已經(jīng)為最終代碼創(chuàng)建了一個(gè)github存儲(chǔ)庫(kù)。

> 獲取示例代碼

多步形式的結(jié)構(gòu)

>我們的工作申請(qǐng)表有四個(gè)部分,其中最後一個(gè)部分是摘要視圖,我們?cè)谔峤恢跋蛴脩粽故玖怂写鸢?。為此,我們將HTML分為四個(gè)部分,每個(gè)部分都用ID識(shí)別,並在頁(yè)面底部添加導(dǎo)航。我將在下一部分給您基線HTML。

>

>導(dǎo)航用戶瀏覽各節(jié)意味著我們還將在剩下的步驟以及剩下多少個(gè)步驟中包含一個(gè)視覺(jué)指示器。該指示器可以是一個(gè)簡(jiǎn)單的動(dòng)態(tài)文本,該文本根據(jù)活動(dòng)步驟或更高的進(jìn)度條形類型的指示器進(jìn)行更新。我們將做前者,以使事情變得簡(jiǎn)單,並專注於形式的多步驟。

結(jié)構(gòu)和基本樣式

>我們將更多地關(guān)注邏輯,但是我將提供代碼片段和末尾的完整代碼的鏈接。

>讓我們從創(chuàng)建一個(gè)文件夾以保存我們的頁(yè)面。然後,創(chuàng)建一個(gè)index.html文件,然後將以下內(nèi)容粘貼到其中:

>

打開(kāi)html

>查看代碼,您可以看到三個(gè)部分和導(dǎo)航組。這些部分包含表單輸入,沒(méi)有本機(jī)形式驗(yàn)證。這是為了使我們更好地控制顯示錯(cuò)誤消息,因?yàn)橹挥性趩螕簟疤峤弧卑粹o時(shí)觸發(fā)本機(jī)表單驗(yàn)證。
<form >
  <section  >
    <div >
      <div >
        <label for="name">Name <span style="color: red;">*</span></label>
        <input type="text"  name="name" placeholder="Enter your name">
      </div>

      <div >
        <label for="idNum">ID number <span style="color: red;">*</span></label>
        <input type="number"  name="idNum" placeholder="Enter your ID number">
      </div>
    </div>

    <div >
      <div >
        <label for="email">Email <span style="color: red;">*</span></label>
        <input type="email"  name="email" placeholder="Enter your email">
      </div>

      <div >
        <label for="birthdate">Date of Birth <span style="color: red;">*</span></label>
        <input type="date"  name="birthdate" max="2006-10-01" min="1924-01-01">
      </div>
    </div>
  </section>

  <section  >
    <div >
      <label for="document">Upload CV <span style="color: red;">*</span></label>
      <input type="file" name="document" >
    </div>

    <div >
      <label for="department">Department <span style="color: red;">*</span></label>
      <select  name="department">
        <option value="">Select a department</option>
        <option value="hr">Human Resources</option>
        <option value="it">Information Technology</option>
        <option value="finance">Finance</option>
      </select>
    </div>
  </section>

  <section  >
    <div >
      <label for="skills">Skills (Optional)</label>
      <textarea  name="skills" rows="4" placeholder="Enter your skills"></textarea>
    </div>

    <div >
      <input type="checkbox" name="terms" >
      <label for="terms">I agree to the terms and conditions <span style="color: red;">*</span></label>
    </div>

    <button  type="submit">Confirm and Submit</button>
  </section>
  
  <div >
    <button type="button" >Previous</button>
    <span ></span>
    <button type="button" >Next</button>
  </div>
</form>

<script src="script.js"></script>
接下來(lái),創(chuàng)建一個(gè)style.css文件並將其粘貼到其中:>
開(kāi)放式基礎(chǔ)樣式
<form >
  <section  >
    <div >
      <div >
        <label for="name">Name <span style="color: red;">*</span></label>
        <input type="text"  name="name" placeholder="Enter your name">
      </div>

      <div >
        <label for="idNum">ID number <span style="color: red;">*</span></label>
        <input type="number"  name="idNum" placeholder="Enter your ID number">
      </div>
    </div>

    <div >
      <div >
        <label for="email">Email <span style="color: red;">*</span></label>
        <input type="email"  name="email" placeholder="Enter your email">
      </div>

      <div >
        <label for="birthdate">Date of Birth <span style="color: red;">*</span></label>
        <input type="date"  name="birthdate" max="2006-10-01" min="1924-01-01">
      </div>
    </div>
  </section>

  <section  >
    <div >
      <label for="document">Upload CV <span style="color: red;">*</span></label>
      <input type="file" name="document" >
    </div>

    <div >
      <label for="department">Department <span style="color: red;">*</span></label>
      <select  name="department">
        <option value="">Select a department</option>
        <option value="hr">Human Resources</option>
        <option value="it">Information Technology</option>
        <option value="finance">Finance</option>
      </select>
    </div>
  </section>

  <section  >
    <div >
      <label for="skills">Skills (Optional)</label>
      <textarea  name="skills" rows="4" placeholder="Enter your skills"></textarea>
    </div>

    <div >
      <input type="checkbox" name="terms" >
      <label for="terms">I agree to the terms and conditions <span style="color: red;">*</span></label>
    </div>

    <button  type="submit">Confirm and Submit</button>
  </section>
  
  <div >
    <button type="button" >Previous</button>
    <span ></span>
    <button type="button" >Next</button>
  </div>
</form>

<script src="script.js"></script>

>打開(kāi)瀏覽器中的HTML文件,您應(yīng)該在以下屏幕截圖中獲得類似兩列佈局的內(nèi)容,並使用當(dāng)前頁(yè)面指示器和導(dǎo)航。

>使用香草JavaScript添加功能

現(xiàn)在,在與HTML和CSS文件相同的目錄中創(chuàng)建一個(gè)腳本。

開(kāi)放基本腳本
:root {
  --primary-color: #8c852a;
  --secondary-color: #858034;
}

body {
  font-family: sans-serif;
  line-height: 1.4;
  margin: 0 auto;
  padding: 20px;
  background-color: #f4f4f4;
  max-width: 600px;
}

h1 {
  text-align: center;
}

form {
  background: #fff;
  padding: 40px;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
}

.form-group {
  display: flex;
  gap: 7%;

}

.form-group > div {
  width: 100%;
}

input:not([type="checkbox"]),
select,
textarea {
  width: 100%;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.form-control {
  margin-bottom: 15px;
}

button {
  display: block;
  width: 100%;
  padding: 10px;
  color: white;
  background-color: var(--primary-color);
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;

}

button:hover {
  background-color: var(--secondary-color);
}

.group-two, .group-three {
  display: none;
}

.arrows {
  display: flex;
  justify-content: space-between
  align-items: center;
  margin-top: 10px;
}

#navLeft, #navRight {
  width: fit-content;
}

@media screen and (max-width: 600px) {
  .form-group {
    flex-direction: column;
  }
}
>此腳本定義了一種方法,該方法根據(jù)與表單部分的IDS相對(duì)應(yīng)的FormStep值顯示並隱藏了部分。它使用表單的當(dāng)前活動(dòng)部分更新stepinfo。此動(dòng)態(tài)文本是用戶的進(jìn)度指標(biāo)。 然後,它添加了等待頁(yè)面加載並單擊“導(dǎo)航”按鈕的邏輯,以使循環(huán)通過(guò)不同的表單部分。如果您刷新頁(yè)面,您會(huì)看到多步表單按預(yù)期工作。
>

多步形式導(dǎo)航

>讓我們更深入地了解上面的JavaScript代碼所做的事情。在UpdateStepvisibility()函數(shù)中,我們首先隱藏所有部分以具有乾淨(jìng)的板巖:>

然後,我們顯示當(dāng)前活動(dòng)的部分:

接下來(lái),我們更新指示器通過(guò)表格進(jìn)行的文本:>

>最後,如果我們處?kù)兜谝徊?,則隱藏上一個(gè)按鈕,如果我們?cè)谧钺嵋徊糠?,則隱藏下一個(gè)按鈕:>
const stepInfo = document.getElementById("stepInfo");
const navLeft = document.getElementById("navLeft");
const navRight = document.getElementById("navRight");
const form = document.getElementById("myForm");
const formSteps = ["one", "two", "three"];

let currentStep = 0;

function updateStepVisibility() {
  formSteps.forEach((step) => {
    document.getElementById(step).style.display = "none";
  });

  document.getElementById(formSteps[currentStep]).style.display = "block";
  stepInfo.textContent = `Step ${currentStep + 1} of ${formSteps.length}`;
  navLeft.style.display = currentStep === 0 ? "none" : "block";
  navRight.style.display =
  currentStep === formSteps.length - 1 ? "none" : "block";
}

document.addEventListener("DOMContentLoaded", () => {
  navLeft.style.display = "none";
  updateStepVisibility();
  navRight.addEventListener("click", () => {
    if (currentStep < formSteps.length - 1) {
      currentStep++;
      updateStepVisibility();
    }
  });

  navLeft.addEventListener("click", () => {
    if (currentStep > 0) {
      currentStep--;
      updateStepVisibility();
    }
  });
});

>讓我們看一下頁(yè)面加載時(shí)會(huì)發(fā)生什麼。我們首先隱藏上一個(gè)按鈕,因?yàn)楸砀裨诘谝徊糠稚霞虞d:

formSteps.forEach((step) => {
  document.getElementById(step).style.display = "none";
});

然後,我們獲取下一個(gè)按鈕,並添加單擊事件,該單擊事件有條件地增加當(dāng)前步驟數(shù),然後調(diào)用updateTepvisibility()函數(shù),然後更新要顯示的新部分:>

document.getElementById(formSteps[currentStep]).style.display = "block";`
>最後,我們抓住上一個(gè)按鈕並做同樣的事情,但反向。在這裡,我們有條件地減少了步驟計(jì)數(shù)並調(diào)用UpdateStepvisibility():>

處理錯(cuò)誤
stepInfo.textContent = `Step ${currentStep + 1} of ${formSteps.length}`;

>您是否曾經(jīng)花了10分鐘的時(shí)間來(lái)填寫表格,只是為了提交並遇到模糊錯(cuò)誤告訴您要糾正這一點(diǎn)嗎?當(dāng)表格立即告訴我有些不對(duì)勁時(shí),我更喜歡它,以便我可以在

>之前糾正它。這就是我們將以我們的形式做的。
navLeft.style.display = currentStep === 0 ? "none" : "block";
navRight.style.display = currentStep === formSteps.length - 1 ? "none" : "block";

>我們的原則是清楚地指出哪些控件有錯(cuò)誤並提供有意義的錯(cuò)誤消息。當(dāng)用戶採(cǎi)取必要的操作時(shí),明確錯(cuò)誤。讓我們?yōu)槲覀兊谋砀裉砑右恍?yàn)證。首先,讓我們抓住必要的輸入元素,然後將其添加到現(xiàn)有的輸入元素:>

然後,添加一個(gè)函數(shù)以驗(yàn)證步驟:
document.addEventListener("DOMContentLoaded", () => {
navLeft.style.display = "none";
updateStepVisibility();
打開(kāi)驗(yàn)證腳本
<form >
  <section  >
    <div >
      <div >
        <label for="name">Name <span style="color: red;">*</span></label>
        <input type="text"  name="name" placeholder="Enter your name">
      </div>

      <div >
        <label for="idNum">ID number <span style="color: red;">*</span></label>
        <input type="number"  name="idNum" placeholder="Enter your ID number">
      </div>
    </div>

    <div >
      <div >
        <label for="email">Email <span style="color: red;">*</span></label>
        <input type="email"  name="email" placeholder="Enter your email">
      </div>

      <div >
        <label for="birthdate">Date of Birth <span style="color: red;">*</span></label>
        <input type="date"  name="birthdate" max="2006-10-01" min="1924-01-01">
      </div>
    </div>
  </section>

  <section  >
    <div >
      <label for="document">Upload CV <span style="color: red;">*</span></label>
      <input type="file" name="document" >
    </div>

    <div >
      <label for="department">Department <span style="color: red;">*</span></label>
      <select  name="department">
        <option value="">Select a department</option>
        <option value="hr">Human Resources</option>
        <option value="it">Information Technology</option>
        <option value="finance">Finance</option>
      </select>
    </div>
  </section>

  <section  >
    <div >
      <label for="skills">Skills (Optional)</label>
      <textarea  name="skills" rows="4" placeholder="Enter your skills"></textarea>
    </div>

    <div >
      <input type="checkbox" name="terms" >
      <label for="terms">I agree to the terms and conditions <span style="color: red;">*</span></label>
    </div>

    <button  type="submit">Confirm and Submit</button>
  </section>
  
  <div >
    <button type="button" >Previous</button>
    <span ></span>
    <button type="button" >Next</button>
  </div>
</form>

<script src="script.js"></script>

>在這裡,我們檢查每個(gè)必需的輸入是否具有一定的值,以及電子郵件輸入是否具有有效輸入。然後,我們相應(yīng)地設(shè)置了Isvalid Boolean。我們還調(diào)用shower()函數(shù),我們尚未定義。

>

>粘貼此代碼上方的valialAteStep()函數(shù):

>
:root {
  --primary-color: #8c852a;
  --secondary-color: #858034;
}

body {
  font-family: sans-serif;
  line-height: 1.4;
  margin: 0 auto;
  padding: 20px;
  background-color: #f4f4f4;
  max-width: 600px;
}

h1 {
  text-align: center;
}

form {
  background: #fff;
  padding: 40px;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
}

.form-group {
  display: flex;
  gap: 7%;

}

.form-group > div {
  width: 100%;
}

input:not([type="checkbox"]),
select,
textarea {
  width: 100%;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.form-control {
  margin-bottom: 15px;
}

button {
  display: block;
  width: 100%;
  padding: 10px;
  color: white;
  background-color: var(--primary-color);
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;

}

button:hover {
  background-color: var(--secondary-color);
}

.group-two, .group-three {
  display: none;
}

.arrows {
  display: flex;
  justify-content: space-between
  align-items: center;
  margin-top: 10px;
}

#navLeft, #navRight {
  width: fit-content;
}

@media screen and (max-width: 600px) {
  .form-group {
    flex-direction: column;
  }
}

現(xiàn)在,將以下樣式添加到樣式表:

打開(kāi)驗(yàn)證樣式
const stepInfo = document.getElementById("stepInfo");
const navLeft = document.getElementById("navLeft");
const navRight = document.getElementById("navRight");
const form = document.getElementById("myForm");
const formSteps = ["one", "two", "three"];

let currentStep = 0;

function updateStepVisibility() {
  formSteps.forEach((step) => {
    document.getElementById(step).style.display = "none";
  });

  document.getElementById(formSteps[currentStep]).style.display = "block";
  stepInfo.textContent = `Step ${currentStep + 1} of ${formSteps.length}`;
  navLeft.style.display = currentStep === 0 ? "none" : "block";
  navRight.style.display =
  currentStep === formSteps.length - 1 ? "none" : "block";
}

document.addEventListener("DOMContentLoaded", () => {
  navLeft.style.display = "none";
  updateStepVisibility();
  navRight.addEventListener("click", () => {
    if (currentStep < formSteps.length - 1) {
      currentStep++;
      updateStepVisibility();
    }
  });

  navLeft.addEventListener("click", () => {
    if (currentStep > 0) {
      currentStep--;
      updateStepVisibility();
    }
  });
});

>如果您刷新表單,您將看到按鈕不會(huì)將您帶到下一部分,直到輸入被認(rèn)為有效:

>最後,我們要添加實(shí)時(shí)錯(cuò)誤處理,以便當(dāng)用戶開(kāi)始輸入正確的信息時(shí),錯(cuò)誤會(huì)消失。在valialAteStep()函數(shù)下方添加此功能:>

打開(kāi)實(shí)時(shí)驗(yàn)證腳本
formSteps.forEach((step) => {
  document.getElementById(step).style.display = "none";
});
>如果輸入不再通過(guò)收聽(tīng)輸入和更改事件而無(wú)效,則此功能會(huì)清除錯(cuò)誤,然後調(diào)用一個(gè)函數(shù)以清除錯(cuò)誤。在淋浴器()下方粘貼clearError()函數(shù):

>

document.getElementById(formSteps[currentStep]).style.display = "block";`
現(xiàn)在,當(dāng)用戶輸入正確的值時(shí),錯(cuò)誤清除了:

現(xiàn)在,多步形式可以優(yōu)雅地處理錯(cuò)誤。如果您確實(shí)決定將錯(cuò)誤保留到表格結(jié)束之前,則至少將用戶跳回錯(cuò)誤的表單控制並顯示了他們需要修復(fù)多少錯(cuò)誤的指示。 >

>

>處理表單提交

>在多步格式中,在提交之前,在結(jié)尾向用戶摘要的所有答案摘要並為他們提供在必要時(shí)編輯答案的選項(xiàng)是有價(jià)值的。該人在不向後導(dǎo)航的情況下看不到前面的步驟,因此在最後一步中顯示摘要會(huì)提供保證和糾正任何錯(cuò)誤的機(jī)會(huì)。

>

>讓我們?cè)跇?biāo)記中添加第四部分,以保留此摘要視圖並在其中移動(dòng)提交按鈕。將此粘貼在index.html的第三部分下方:

打開(kāi)html

然後在JavaScript中更新formStep以讀?。?ancy>>
stepInfo.textContent = `Step ${currentStep + 1} of ${formSteps.length}`;
>最後,將以下類添加到styles.css:

navLeft.style.display = currentStep === 0 ? "none" : "block";
navRight.style.display = currentStep === formSteps.length - 1 ? "none" : "block";
現(xiàn)在,將以下內(nèi)容添加到腳本的頂部。

然後在scripts.js中添加此函數(shù):
document.addEventListener("DOMContentLoaded", () => {
navLeft.style.display = "none";
updateStepVisibility();

>這將輸入值動(dòng)態(tài)插入表單的摘要部分,截?cái)辔募瑏K為輸入提供了後備文本。
navRight.addEventListener("click", () => {
  if (currentStep < formSteps.length - 1) {
    currentStep++;
    updateStepVisibility();
  }
});
然後更新UpdateStepvisibility()函數(shù)以調(diào)用新功能:>

最後,將其添加到domcontentloaded事件偵聽(tīng)器:>

<form >
  <section  >
    <div >
      <div >
        <label for="name">Name <span style="color: red;">*</span></label>
        <input type="text"  name="name" placeholder="Enter your name">
      </div>

      <div >
        <label for="idNum">ID number <span style="color: red;">*</span></label>
        <input type="number"  name="idNum" placeholder="Enter your ID number">
      </div>
    </div>

    <div >
      <div >
        <label for="email">Email <span style="color: red;">*</span></label>
        <input type="email"  name="email" placeholder="Enter your email">
      </div>

      <div >
        <label for="birthdate">Date of Birth <span style="color: red;">*</span></label>
        <input type="date"  name="birthdate" max="2006-10-01" min="1924-01-01">
      </div>
    </div>
  </section>

  <section  >
    <div >
      <label for="document">Upload CV <span style="color: red;">*</span></label>
      <input type="file" name="document" >
    </div>

    <div >
      <label for="department">Department <span style="color: red;">*</span></label>
      <select  name="department">
        <option value="">Select a department</option>
        <option value="hr">Human Resources</option>
        <option value="it">Information Technology</option>
        <option value="finance">Finance</option>
      </select>
    </div>
  </section>

  <section  >
    <div >
      <label for="skills">Skills (Optional)</label>
      <textarea  name="skills" rows="4" placeholder="Enter your skills"></textarea>
    </div>

    <div >
      <input type="checkbox" name="terms" >
      <label for="terms">I agree to the terms and conditions <span style="color: red;">*</span></label>
    </div>

    <button  type="submit">Confirm and Submit</button>
  </section>
  
  <div >
    <button type="button" >Previous</button>
    <span ></span>
    <button type="button" >Next</button>
  </div>
</form>

<script src="script.js"></script>

>運(yùn)行表格,您應(yīng)該看到摘要部分顯示所有輸入的值,並允許用戶在提交信息之前編輯任何內(nèi)容:

現(xiàn)在,我們可以提交我們的表格:

:root {
  --primary-color: #8c852a;
  --secondary-color: #858034;
}

body {
  font-family: sans-serif;
  line-height: 1.4;
  margin: 0 auto;
  padding: 20px;
  background-color: #f4f4f4;
  max-width: 600px;
}

h1 {
  text-align: center;
}

form {
  background: #fff;
  padding: 40px;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
}

.form-group {
  display: flex;
  gap: 7%;

}

.form-group > div {
  width: 100%;
}

input:not([type="checkbox"]),
select,
textarea {
  width: 100%;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.form-control {
  margin-bottom: 15px;
}

button {
  display: block;
  width: 100%;
  padding: 10px;
  color: white;
  background-color: var(--primary-color);
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;

}

button:hover {
  background-color: var(--secondary-color);
}

.group-two, .group-three {
  display: none;
}

.arrows {
  display: flex;
  justify-content: space-between
  align-items: center;
  margin-top: 10px;
}

#navLeft, #navRight {
  width: fit-content;
}

@media screen and (max-width: 600px) {
  .form-group {
    flex-direction: column;
  }
}

我們的多步表格現(xiàn)在允許用戶在提交之前編輯並查看其提供的所有信息。

>可訪問(wèn)性提示

使多步式形式訪問(wèn)以基礎(chǔ)知識(shí)為開(kāi)始:使用語(yǔ)義html。 這是戰(zhàn)鬥的一半。緊隨其後的是使用適當(dāng)?shù)谋韱螛?biāo)籤。

使表單更容易訪問(wèn)的其他方法包括給必須在小屏幕上單擊的元素提供足夠的空間,並對(duì)錶單導(dǎo)航和進(jìn)度指標(biāo)進(jìn)行有意義的描述。

> 向用戶提供反饋是其中的重要組成部分;在一定的時(shí)間後自動(dòng)自動(dòng)使用用戶反饋並不是很棒,但是允許用戶自己解散。注意對(duì)比和字體選擇也很重要,因?yàn)樗鼈兌紩?huì)影響您的形式的可讀性。

>讓我們對(duì)標(biāo)記進(jìn)行以下調(diào)整,以獲得更多技術(shù)可訪問(wèn)性:

>

>向所有輸入添加aria-required =“ true”。

>
    >添加角色=“警報(bào)”到錯(cuò)誤跨度。
  1. 這有助於屏幕讀者知道在輸入處?kù)跺e(cuò)誤狀態(tài)時(shí)具有重要的意義。 > >
  2. 添加角色=“狀態(tài)” aria-live =“ polite”到.stepinfo。
  3. 這將有助於屏幕讀者了解,步驟信息將標(biāo)籤保持在狀態(tài)上,並且將Aria live設(shè)置為有禮貌,表明值得更改價(jià)值,它無(wú)需立即將其啟用。 >> >
  4. >>
  5. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>。 在腳本文件中,用以下內(nèi)容替換showerror()和clearError()函數(shù):

>在這裡,我們通過(guò)編程添加和刪除以其錯(cuò)誤跨度明確連接輸入的屬性,並表明其處?kù)稛o(wú)效狀態(tài)。

>最後,讓我們將重點(diǎn)放在每個(gè)部分的第一個(gè)輸入上;將以下代碼添加到updateStepvisibility()函數(shù)的末尾:>
const stepInfo = document.getElementById("stepInfo");
const navLeft = document.getElementById("navLeft");
const navRight = document.getElementById("navRight");
const form = document.getElementById("myForm");
const formSteps = ["one", "two", "three"];

let currentStep = 0;

function updateStepVisibility() {
  formSteps.forEach((step) => {
    document.getElementById(step).style.display = "none";
  });

  document.getElementById(formSteps[currentStep]).style.display = "block";
  stepInfo.textContent = `Step ${currentStep + 1} of ${formSteps.length}`;
  navLeft.style.display = currentStep === 0 ? "none" : "block";
  navRight.style.display =
  currentStep === formSteps.length - 1 ? "none" : "block";
}

document.addEventListener("DOMContentLoaded", () => {
  navLeft.style.display = "none";
  updateStepVisibility();
  navRight.addEventListener("click", () => {
    if (currentStep < formSteps.length - 1) {
      currentStep++;
      updateStepVisibility();
    }
  });

  navLeft.addEventListener("click", () => {
    if (currentStep > 0) {
      currentStep--;
      updateStepVisibility();
    }
  });
});

>,多步驟形式更容易訪問(wèn)。

結(jié)論

>我們走了,為工作應(yīng)用程序提供了四部分的多步格式!正如我在本文頂部所說(shuō)的那樣,有很多事情要做 - 如此之多,以至於我不會(huì)因?yàn)閷ふ议_(kāi)箱即用的解決方案而對(duì)。
formSteps.forEach((step) => {
  document.getElementById(step).style.display = "none";
});
>但是,如果您必須瀏覽多步驟的形式,希望現(xiàn)在您會(huì)發(fā)現(xiàn)這不是死刑判決。有一條愉快的道路可以讓您到達(dá)那裡,並進(jìn)行導(dǎo)航和驗(yàn)證,而不會(huì)遠(yuǎn)離良好的,可訪問(wèn)的做法。

這就是我接近它的方式!同樣,我將此作為個(gè)人挑戰(zhàn),以了解我能走多遠(yuǎn),我對(duì)此非常滿意。但是,我很想知道您是否看到了其他機(jī)會(huì),使這更加註意用戶體驗(yàn)和體貼可訪問(wèn)性。

參考

這是我在撰寫本文時(shí)提到的一些相關(guān)鏈接:>

    >如何構(gòu)建Web表單(MDN)
  1. >多頁(yè)式(w3c.org)
  2. 創(chuàng)建可訪問(wèn)的表單(A11Y項(xiàng)目)

以上是如何使用香草JavaScript和CSS創(chuàng)建多步驟形式的詳細(xì)內(nèi)容。更多資訊請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請(qǐng)聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開(kāi)發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺(jué)化網(wǎng)頁(yè)開(kāi)發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)程式碼編輯軟體(SublimeText3)

什麼是'渲染障礙CSS”? 什麼是'渲染障礙CSS”? Jun 24, 2025 am 12:42 AM

CSS會(huì)阻塞頁(yè)面渲染是因?yàn)闉g覽器默認(rèn)將內(nèi)聯(lián)和外部CSS視為關(guān)鍵資源,尤其是使用引入的樣式表、頭部大量?jī)?nèi)聯(lián)CSS以及未優(yōu)化的媒體查詢樣式。 1.提取關(guān)鍵CSS並內(nèi)嵌至HTML;2.延遲加載非關(guān)鍵CSS通過(guò)JavaScript;3.使用media屬性優(yōu)化加載如打印樣式;4.壓縮合併CSS減少請(qǐng)求。建議使用工具提取關(guān)鍵CSS,結(jié)合rel="preload"異步加載,合理使用media延遲加載,避免過(guò)度拆分與復(fù)雜腳本控制。

外部與內(nèi)部CSS:最好的方法是什麼? 外部與內(nèi)部CSS:最好的方法是什麼? Jun 20, 2025 am 12:45 AM

thebestapphachforcssdepprodsontheproject'sspefificneeds.forlargerprojects,externalcsSissBetterDuoSmaintoMaintainability andReusability; forsMallerProjectsorsingle-pageApplications,InternaltCsmightBemoresobleable.InternalCsmightBemorese.it.it'sclucialtobalancepopryseceneceenceprodrenceprodrenceNeed

CSS案例靈敏度:了解重要的 CSS案例靈敏度:了解重要的 Jun 20, 2025 am 12:09 AM

cssismostlycaseminemintiment,buturlsandfontfamilynamesarecase敏感。 1)屬性和valueslikeColor:紅色; prenotcase-sensive.2)urlsmustmustmatchtheserver'server'scase,例如

什麼是AutoPrefixer,它如何工作? 什麼是AutoPrefixer,它如何工作? Jul 02, 2025 am 01:15 AM

Autoprefixer是一個(gè)根據(jù)目標(biāo)瀏覽器範(fàn)圍自動(dòng)為CSS屬性添加廠商前綴的工具。 1.它解決了手動(dòng)維護(hù)前綴易出錯(cuò)的問(wèn)題;2.通過(guò)PostCSS插件形式工作,解析CSS、分析需加前綴的屬性、依配置生成代碼;3.使用步驟包括安裝插件、設(shè)置browserslist、在構(gòu)建流程中啟用;4.注意事項(xiàng)有不手動(dòng)加前綴、保持配置更新、非所有屬性都加前綴、建議配合預(yù)處理器使用。

什麼是圓錐級(jí)函數(shù)? 什麼是圓錐級(jí)函數(shù)? Jul 01, 2025 am 01:16 AM

theconic-Gradient()functionIncsscreatesCircularGradientsThatRotateColorStopSaroundAcentralPoint.1.IsidealForPieCharts,ProgressIndicators,colordichers,colorwheels和decorativeBackgrounds.2.itworksbysbysbysbydefindefingincolordefingincolorstopsatspecificains off.

CSS教程,用於創(chuàng)建粘性標(biāo)頭或頁(yè)腳 CSS教程,用於創(chuàng)建粘性標(biāo)頭或頁(yè)腳 Jul 02, 2025 am 01:04 AM

TocreatestickyheadersandfooterswithCSS,useposition:stickyforheaderswithtopvalueandz-index,ensuringparentcontainersdon’trestrictit.1.Forstickyheaders:setposition:sticky,top:0,z-index,andbackgroundcolor.2.Forstickyfooters,betteruseposition:fixedwithbot

CSS自定義屬性的範(fàn)圍是什麼? CSS自定義屬性的範(fàn)圍是什麼? Jun 25, 2025 am 12:16 AM

CSS自定義屬性的作用域取決於其聲明的上下文,全局變量通常定義在:root中,而局部變量則定義在特定選擇器內(nèi),以便組件化和隔離樣式。例如,定義在.card類中的變量?jī)H對(duì)匹配該類的元素及其子元素可用。最佳實(shí)踐包括:1.使用:root定義全局變量如主題色;2.在組件內(nèi)部定義局部變量以實(shí)現(xiàn)封裝;3.避免重複聲明同一變量;4.注意選擇器特異性可能引發(fā)的覆蓋問(wèn)題。此外,CSS變量區(qū)分大小寫,且應(yīng)在使用前定義以避免錯(cuò)誤。若變量未定義或引用失敗,則會(huì)採(cǎi)用回退值或默認(rèn)值initial。調(diào)試時(shí)可通過(guò)瀏覽器開(kāi)發(fā)者工

解鎖CSS動(dòng)畫的潛力:深度潛水 解鎖CSS動(dòng)畫的潛力:深度潛水 Jun 20, 2025 am 12:14 AM

cssanimationsenhancewebpagesbyimprovinguserexperienceandsiteFunctionallity.1)usetransitionsforsmoothstylechanges,asinthebuttoncolorexample.2)losplyKeyKeyframesforeSfordEteTailEdAnimations.3)

See all articles