简体   繁体   English

多步骤表单Javascript

[英]Multi Step Form Javascript

I am trying to create a multi step form for a website I am doing. 我正在尝试为我正在做的网站创建一个多步骤表单。 The website is an autobody site, so theres a check box for services that a car would need. 该网站是车身网站,因此有一个复选框,用于提供汽车所需的服务。 Such as oil change, tire rotation, echeck, and then other. 例如换油,轮胎旋转,echeck等。 Under "other", i have a text input box, but i don't want it to be active unless "other" is checked. 在“其他”下,我有一个文本输入框,但是除非选中“其他”,否则我不希望它处于活动状态。 With my current JS, I have it so the user can't go on to the next step of the form until all fields are filled in. But the code is detecting the "other" text box when it's not checked and wont let me go on to the next step. 使用当前的JS,我可以使用它,以便用户在填写完所有字段之前无法继续进行表单的下一步。但是,如果未选中该代码,代码将检测到“其他”文本框,并且不会让我继续继续下一步。 If someone could look at my code and see what I am doing wrong, that would be great. 如果有人可以查看我的代码并看到我在做错什么,那将很棒。

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;
 }
};

To me it looks like the problem is that you're getting all the inputs whether they're disabled or not, so you need to take that into account. 对我来说,问题似乎在于无论是否禁用,您都在获取所有输入,因此您需要考虑到这一点。 Since you're using vanilla JS, you could do something like this with that IF condition in the validateForm function: 由于您使用的是香草JS,因此可以在validateForm函数中使用该IF条件执行以下操作:

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

This way it will only pick up the input fields that aren't disabled. 这样,它将仅拾取未禁用的输入字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM