繁体   English   中英

JavaScript 验证 Select 盒子

[英]JavaScript Validate Select Box

我使用此解决方案制作了一个多步骤表单。 https://www.w3schools.com/howto/howto_js_form_steps.asp

唯一的区别是此表单中需要的几个字段位于 select 框中,出于某种原因,我无法根据查找 select 选项值的值来触发验证处理程序。

我想检查并确保表单中的指定字段不会返回空字符串,就像 validateForm() function 中的输入字段一样。

这是JS代码。 我在标记中表单的每个部分都使用相同的“制表符”class。 只有脚本需要验证处理。

var currentTab = 0; // Current tab is set to be the first tab (0)
showTab(currentTab); // Display the current tab

function showTab(n) {
  // This function will display the specified tab of the form ...
  var x = document.getElementsByClassName("tab");
  x[n].style.display = "block";
  // ... and fix the Previous/Next buttons:
  if (n == 0) {
    document.getElementById("nextBtn").style.display = "inline";
  } else {
  }
  if (n == (x.length - 1)) {
    document.getElementById("nextBtn").style.display = "none";
  } else {
    document.getElementById("nextBtn").innerHTML = "Next";
  }
  // ... and run a function that displays the correct step indicator:
  fixStepIndicator(n)
}

function nextPrev(n) {
  // This function will figure out which tab to display
  var x = document.getElementsByClassName("tab");
  // Exit the function if any field in the current tab is invalid:
  // Hide the current tab:
  x[currentTab].style.display = "none";
  // Increase or decrease the current tab by 1:
  currentTab = currentTab + n;
  // if you have reached the end of the form... :
  if (currentTab >= x.length) {
    //...the form gets submitted:
    document.getElementById("regForm").submit();
    return false;
  }
  // Otherwise, display the correct tab:
  showTab(currentTab);
}

function validateForm() {
  // This function deals with validation of the form fields
  var x, y, i, q, optCheck, valid = true;
  x = document.getElementsByClassName("tab");
  y = x[currentTab].getElementsByTagName("input");
  q = x[currentTab].getElementById("required-select");

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

  for(i = 0; q.length; i++){
    optCheck = document.getElementsByTagName('option')[q][i].value;

    if(optCheck.value == ""){
      q[i].className += "invalid";
      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";
}

我的头有点像 web。有人可以帮忙吗?

谢谢:! :D

没关系。 我已经想通了。 脑放屁!

我需要像输入字段一样遍历 select 字段。

因此,创建一个变量并通过将变量的值分配给当前选项卡并获取标签名称(选择)来复制对输入字段所做的操作,然后编写一个 for 循环来检查每个当前选项卡以查看是否有值通过。

像冠军一样工作!

暂无
暂无

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

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