如果单击按钮,则显示两种形式之一

时间:2018-12-01 20:11:29

标签: javascript jquery forms

我需要一些帮助。我想创建一个具有两个按钮的网页,具体取决于选择的按钮是我要显示的是表单1还是表单2。接下来的事情是,如果用户选择了表单1的按钮,我只想对表单1进行表单验证选择的形式。

// Example starter JavaScript for disabling form submissions if there are invalid fields
(function() {
  'use strict';
  window.addEventListener('load', function() {
    // Fetch all the forms we want to apply custom Bootstrap validation styles to
    var forms = document.getElementsByClassName('needs-validation');
    // Loop over them and prevent submission
    var validation = Array.prototype.filter.call(forms, function(form) {
      form.addEventListener('submit', function(event) {
        if (form.checkValidity() === false) {
          event.preventDefault();
          event.stopPropagation();
        }
        form.classList.add('was-validated');
      }, false);
    });
  }, false);
})();
<button type="button" class="btn btn-outline-primary">show form 1</button>
<button type="button" class="btn btn-outline-primary">show form 2</button>

<br>

<form id="form1" class="needs-validation" novalidate>
  <hr>
  <p>THIS IS FORM 1</p>
  <hr>
  <div class="form-row">
    <div class="col-md-4 mb-3">
      <label for="validationCustom01">form1 box 1</label>
      <input type="text" class="form-control" id="validationCustom01" placeholder="form1 box 1" required>
      <div class="valid-feedback">
        Looks good!
      </div>
    </div>
    <div class="col-md-4 mb-3">
      <label for="validationCustom02">form1 box 2</label>
      <input type="text" class="form-control" id="validationCustom02" placeholder="form1 box 2" required>
      <div class="valid-feedback">
        Looks good!
      </div>
    </div>

  </div>
  <button class="btn btn-primary" type="submit">Submit form1</button>
</form>

<br>

<form id="form2" class="needs-validation" novalidate>
  <hr>
  <p>THIS IS FORM 2</p>
  <hr>
  <div class="form-row">
    <div class="col-md-4 mb-3">
      <label for="validationCustom01">form2 box 1</label>
      <input type="text" class="form-control" id="validationCustom01" placeholder="form1 box 1" required>
      <div class="valid-feedback">
        Looks good!
      </div>
    </div>
    <div class="col-md-4 mb-3">
      <label for="validationCustom02">form2 box 2</label>
      <input type="text" class="form-control" id="validationCustom02" placeholder="form1 box 2" required>
      <div class="valid-feedback">
        Looks good!
      </div>
    </div>

  </div>
  <button class="btn btn-primary" type="submit">Submit form2</button>
</form>

1 个答案:

答案 0 :(得分:0)

查看它是否符合您的要求。

我刚刚向按钮添加了一个事件,该事件仅翻转了两种形式的显示属性。 为了进行验证,仅对显示的表单进行验证,但是一旦您验证了表单,它就仍然保持验证状态。

// Example starter JavaScript for disabling form submissions if there are invalid fields
(function() {
  'use strict';
  window.addEventListener('load', function() {
    // Fetch all the forms we want to apply custom Bootstrap validation styles to
    var forms = document.getElementsByClassName('needs-validation');
    // Loop over them and prevent submission
    var validation = Array.prototype.filter.call(forms, function(form) {
      form.addEventListener('submit', function(event) {
        if (form.checkValidity() === false) {
          event.preventDefault();
          event.stopPropagation();
        }
        form.classList.add('was-validated');
      }, false);
    });
  }, false);
})();

function toggle(i) {
  let form1 = document.getElementById('form1');
  let form2 = document.getElementById('form2');

  if (i == 1) {
    form1.style.display = 'none';
    form2.style.display = 'block';
  } else {
    form1.style.display = 'block';
    form2.style.display = 'none';
  }
}
.row {
  background: #f8f9fa;
  margin-top: 20px;
}

.col {
  border: solid 1px #6c757d;
  padding: 10px;
}
<button type="button" id="btn1" class="btn btn-outline-primary" onclick="toggle(2)">show form 1</button>
<button type="button" class="btn btn-outline-primary" onclick="toggle(1)">show form 2</button>
<br><br>


<form id="form1" class="needs-validation" novalidate>
  <hr>
  <p>THIS IS FORM 1</p>
  <hr>
  <div class="form-row">
    <div class="col-md-4 mb-3">
      <label for="validationCustom01">form1 box 1</label>
      <input type="text" class="form-control" id="validationCustom01" placeholder="form1 box 1" required>
      <div class="valid-feedback">
        Looks good!
      </div>
    </div>
    <div class="col-md-4 mb-3">
      <label for="validationCustom02">form1 box 2</label>
      <input type="text" class="form-control" id="validationCustom02" placeholder="form1 box 2" required>
      <div class="valid-feedback">
        Looks good!
      </div>
    </div>

  </div>
  <button class="btn btn-primary" type="submit">Submit form1</button>
</form>

<form id="form2" class="needs-validation" style="display:none" novalidate>
  <hr>
  <p>THIS IS FORM 2</p>
  <hr>
  <div class="form-row">
    <div class="col-md-4 mb-3">
      <label for="validationCustom01">form2 box 1</label>
      <input type="text" class="form-control" id="validationCustom01" placeholder="form1 box 1" required>
      <div class="valid-feedback">
        Looks good!
      </div>
    </div>
    <div class="col-md-4 mb-3">
      <label for="validationCustom02">form2 box 2</label>
      <input type="text" class="form-control" id="validationCustom02" placeholder="form1 box 2" required>
      <div class="valid-feedback">
        Looks good!
      </div>
    </div>

  </div>
  <button class="btn btn-primary" type="submit">Submit form2</button>
</form>

对于这类事情,我建议您顺便使用某种框架,例如,或者也可以使用进行简单的事情。