簡體   English   中英

多步驟表單Javascript

[英]Multi Step Form Javascript

我正在嘗試為我正在做的網站創建一個多步驟表單。 該網站是車身網站,因此有一個復選框,用於提供汽車所需的服務。 例如換油,輪胎旋轉,echeck等。 在“其他”下,我有一個文本輸入框,但是除非選中“其他”,否則我不希望它處於活動狀態。 使用當前的JS,我可以使用它,以便用戶在填寫完所有字段之前無法繼續進行表單的下一步。但是,如果未選中該代碼,代碼將檢測到“其他”文本框,並且不會讓我繼續繼續下一步。 如果有人可以查看我的代碼並看到我在做錯什么,那將很棒。

function validateForm() {
  // This function deals with validation of the form fields
  var x, y, i, valid = true;
  x = document.getElementsByClassName("tab");
  y = x[currentTab].getElementsByTagName("input");
  // A loop that checks every input field in the current tab:
  for (i = 0; i < y.length; i++) {
    // If a field is empty...
    if (y[i].value == "") {
      // add an "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 finished and valid:
  if (valid) {
    document.getElementsByClassName("step")[currentTab].className += " finish";
  }
  return valid; // return the valid status
}

function fixStepIndicator(n) {
  // This function removes the "active" class of all steps...
  var i, x = document.getElementsByClassName("step");
  for (i = 0; i < x.length; i++) {
    x[i].className = x[i].className.replace(" active", "");
  }
  //... and adds the "active" class to the current step:
  x[n].className += " active";
}

//check box other
document.getElementById('other').onchange = function() {
 if(this.checked==true){
  document.getElementById("othertext").disabled=false;
  document.getElementById("othertext").focus();
 }
 else{
  document.getElementById("othertext").disabled=true;
 }
};

對我來說,問題似乎在於無論是否禁用,您都在獲取所有輸入,因此您需要考慮到這一點。 由於您使用的是香草JS,因此可以在validateForm函數中使用該IF條件執行以下操作:

if (y[i].value == "" && y[i].disabled === false) {

這樣,它將僅拾取未禁用的輸入字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM