【问题标题】:Contact Form Redirecting Prevent Default Not Working联系表格重定向防止默认不工作
【发布时间】:2018-03-17 04:02:25
【问题描述】:

我已经尝试了很多不同的方法...不知道为什么仍然会重定向。我想过去我一直使用按钮而不是提交输入,因此我从未遇到过这个问题。但是,我认为是时候深入了解这一点了!

HTML 表单

<form class="col-xs-12" action="mail.php" method="POST" >
  <h2 class="headerFont">Contact</h2>
  <p>Use the form below to send to contact me via email. I will be in touch soon after receiving your message.</p>
  <div class="row">
    <div class="col-xs-12 col-sm-6">
        <input class="col-xs-12" placeholder="Full Name" title="Enter Full Name" type="text" name="name">
        <input class="col-xs-6" placeholder="Email Address" title="Enter Email Address" type="email" name="email">
        <input class="col-xs-6" placeholder="Mobile Phone Number" title="Enter Mobile Phone Number" type="tel" name="phone">
        <input class="col-xs-12" placeholder="Street Address" title="Enter Street Address" type="text" name="address">
        <input type="text" name="_gotcha" id="_gotcha" style="display: none !important">
        <select class="col-xs-12" name="service">
            <option selected disabled>Select Service</option>
            <option>Group Walking</option>
            <option>Private Walking</option>
            <option>Pet Sitting</option>
        </select>
    </div>
    <div class="col-xs-12 col-sm-6">
        <textarea class="col-xs-12" placeholder="Message Here" rows="10" name="message"></textarea>
    </div>
  </div>
  <input type="submit" value="Send" onclick="formSubmit(e)">
</form>

JAVASCRIPT 代码

function formSubmit(e) {

        e.preventDefault();
        return false;

        console.log("Ajax Init");

        var form = e.target,
            data = new FormData(),
            xhr = new XMLHttpRequest();

        for (var i = 0, ii = form.length - 1; i < ii; ++i) {
            var input = form[i];
            data.append(input.name, input.value);
            if (input.getAttribute("name") !== "_gotcha") {
                if (input.value === "" || input.value === null || input.value === "undefined") {
                    alert("Please fill out all form fields before submitting");
                    break;
                }
            }           
        }

        xhr.open(form.method.toUpperCase(), form.action, true);
        if (document.getElementById("_gotcha").value.length == 0){
            xhr.send(data);
        } else {
            break;
        }

        xhr.onloadend = function () {
            // done
            for (var i = 0, ii = form.length - 1; i < ii; ++i) {
                var input = form[i];
                input.value = "";           
            }

            alert("Message Sent - Thank You");
        };

    };

【问题讨论】:

  • 你想预先指定你只需要在javascript中
  • 如果我用纯 Javascript 编写所有代码,可以安全地假设我正在寻找仅 Javascript 的解决方案。就像使用 React 编写反应代码的人不想要 Angular 解决方案一样。

标签: javascript ajax preventdefault


【解决方案1】:

似乎更好的选择是使用onsubmit attribute

function formSubmit(form) {

  console.log("Ajax Init");

  var data = new FormData(form), // simpler
    xhr = new XMLHttpRequest();

  for (var i = 0, ii = form.length - 1; i < ii; ++i) {
    var input = form[i];
    //data.append(input.name, input.value);
    if (input.getAttribute("name") !== "_gotcha") {
      if (input.value === "" || input.value === null || input.value === "undefined") {
        alert("Please fill out all form fields before submitting");
        // something went wrong, prevent form from submitting
        return false;
      }
    }
  }

  xhr.open(form.method.toUpperCase(), form.action, true);
  if (document.getElementById("_gotcha").value.length == 0) {
    xhr.send(data);
  } else {
    // something went wrong, prevent form from submitting
    return false;
  }

  xhr.onloadend = function() {
    // done
    for (var i = 0, ii = form.length - 1; i < ii; ++i) {
      var input = form[i];
      input.value = "";
    }

    alert("Message Sent - Thank You");
  };


  // everything went ok, submit form
  return true;
};
<!-- note the use of return -->
<form class="col-xs-12" action="mail.php" method="POST" onsubmit="return formSubmit(this)">
  <h2 class="headerFont">Contact</h2>
  <p>Use the form below to send to contact me via email. I will be in touch soon after receiving your message.</p>
  <div class="row">
    <div class="col-xs-12 col-sm-6">
      <input class="col-xs-12" placeholder="Full Name" title="Enter Full Name" type="text" name="name">
      <input class="col-xs-6" placeholder="Email Address" title="Enter Email Address" type="email" name="email">
      <input class="col-xs-6" placeholder="Mobile Phone Number" title="Enter Mobile Phone Number" type="tel" name="phone">
      <input class="col-xs-12" placeholder="Street Address" title="Enter Street Address" type="text" name="address">
      <input type="text" name="_gotcha" id="_gotcha" style="display: none !important">
      <select class="col-xs-12" name="service">
            <option selected disabled>Select Service</option>
            <option>Group Walking</option>
            <option>Private Walking</option>
            <option>Pet Sitting</option>
        </select>
    </div>
    <div class="col-xs-12 col-sm-6">
      <textarea class="col-xs-12" placeholder="Message Here" rows="10" name="message"></textarea>
    </div>
  </div>
  <!-- upon clicking on the submit button, it will trigger the form's onsubmit handler -->
  <input type="submit" value="Send">
</form>

【讨论】:

  • 正确,正确的用法是return formSubmit(this),它也适用于onclick
【解决方案2】:

我建议在核心 javascript 中使用 jquery,因为在 javascript 中它会混合想要编写的代码,我在 jquery 中为您编写

第 1 步::为表单标签提供 id id="myForm"

第 2 步::这样编写脚本

<script>
  $('#myForm').submit(function(e){

          e.preventDefualt();
          data = $(this)..serialize();

 });
</script>

【讨论】:

  • 我知道如何使用 jQuery,我选择不使用,因为我可以加载更少的文件。 Javascript很好。
猜你喜欢
  • 1970-01-01
  • 2013-09-02
  • 1970-01-01
  • 2012-07-22
  • 2012-10-04
  • 2017-03-10
  • 1970-01-01
  • 1970-01-01
  • 2015-04-13
相关资源
最近更新 更多