Javascript 在提交多页表单之前,是否需要添加功能以确保至少选中1个复选框?

Javascript 在提交多页表单之前,是否需要添加功能以确保至少选中1个复选框?,javascript,html,Javascript,Html,我正在处理一个有多页的表单。在一个页面上,我有一些复选框,我需要在提交之前至少选择一个复选框。如何使用javascript/html5实现这一点 这是一个用javascript改变页面的表单。如果至少选择了一个复选框,如何在最后一页进行表单检查 var currentTab = 0; // Current tab is set to be the first tab (0) showTab(currentTab); // Display the current tab function sho

我正在处理一个有多页的表单。在一个页面上,我有一些复选框,我需要在提交之前至少选择一个复选框。如何使用javascript/html5实现这一点

这是一个用javascript改变页面的表单。如果至少选择了一个复选框,如何在最后一页进行表单检查

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("prevBtn").style.display = "none";
  } else {
    document.getElementById("prevBtn").style.display = "inline";
  }
  if (n == (x.length - 1)) {
    document.getElementById("nextBtn").innerHTML = "Submit";
  } else {
    document.getElementById("nextBtn").innerHTML = "Next";
  }
  //... and run a function that will display 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:
  if (n == 1 && !validateForm()) return false;
  // 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, 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 on the current step:
  x[n].className += " active";
}

输入第二个类名

此代码获取该元素并对全局变量的输入进行计数

var checkboxes = document.querySelectorAll("tab.theFirstTabWithCheckboxes input");

var i = checkboxes.length;

window.count_nr_of_checked_boxes = 0;
while (i--) { //count down to 0
  if (checkboxes[i].checked) window.count_nr_of_checked_boxes++;
}
与您在CSS中选择的方式相同。请注意,空格运算符如何表示输入在。。。它是从右向左计算的

在函数validateForm中放入以下行

if (count_nr_of_checked_boxes < 1) valid = false
你不需要有窗户。关于全局变量

很抱歉,我在while-I中输入了一个错误,我将其编辑为whilei-。然后它在条件之后而不是之前递减。例如,如果i=10,它将倒计时,因此语句中的第一个i是9,最后一个i是0。完美的索引数组,其中向后操作通常很好,您不需要记住Array.length上的-1。while循环在看到i==0时停止。
var checkboxes = document.querySelectorAll("tab.theFirstTabWithCheckboxes input");

var i = checkboxes.length;

window.count_nr_of_checked_boxes = 0;
while (i--) { //count down to 0
  if (checkboxes[i].checked) window.count_nr_of_checked_boxes++;
}
if (count_nr_of_checked_boxes < 1) valid = false