Submit form using ajax using bootstrap

我的梦境 提交于 2020-01-22 02:33:05

问题


So I'm attempting to submit my form using ajax but I'm running into some issues where it's not submitting at all - Could someone explain to me what I might be doing wrong?

(function() {
  'use strict'

  window.addEventListener('load', function() {
    // Fetch all the forms we want to apply custom Bootstrap validation styles to
    const forms = document.getElementsByClassName('needs-validation')

    // Loop over them and prevent submission
    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)
}());
<form class="needs-validation" id="contactForm" action="/" method="post" novalidate>
  <h4 class="mt-5">Please provide your contact information</h4>

  <div class="row mt-3">
    <div class="col-md-4 mt-3">
      <label for="firstName">First name</label>
      <input type="text" class="form-control" id="firstName" name="contact[firstName]" required>
      <div class="invalid-feedback">
        First name is required.
      </div>
    </div>
  </div>

  <div class="row">
    <div class="col-md">
      <button class="btn btn-primary" type="submit">Submit Request</button>
    </div>
  </div>
</form>

来源:https://stackoverflow.com/questions/59549973/submit-form-using-ajax-using-bootstrap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!