js & HTML форма с несколькими шагами из w3school не работает на 'input type = radio' - PullRequest
0 голосов
/ 27 апреля 2020

Отсюда я получил форму https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_form_steps

что я пытаюсь сделать

Я добавил эти две вкладки

<div class="tab">
    <input type="radio" name="question1" oninput="this.className = ''" value="answer1">
    <input type="radio" name="question1" oninput="this.className = ''" value="answer2"> 
</div>
<div class="tab">
    <input type="radio" name="question2" oninput="this.className = ''" value="answer1">
    <input type="radio" name="question2" oninput="this.className = ''" value="answer2"> 
</div>

и в коде javascript в "function validateform" Вместо этого

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
}

я отредактировал и вставил javascript, как показано ниже:

y = x[currentTab].getElementsByName("question");

и

for (i = 0; i < y.length; i++) {
    // If a field is empty...
    if (y[i].checked == false) {
      // add an "invalid" class to the field:
      y[i].className += " invalid";
      // and set the current valid status to false
      valid = false;
    }
  }

И это не сработает.

пожалуйста, ребята, вы можете мне помочь?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...