简体   繁体   English

使用e.preventDefault();的联系表; 没有现场工作

[英]Contact form using e.preventDefault(); not working live

UPDATE - the contact form is found at this URL . UPDATE-在此URL上找到联系表格。

I am trying to get the following contact form to function, using this tutorial . 我正在尝试使用本教程使以下联系表起作用。

I manage to get everything to work as expected on my computer using apache webserver. 我设法使用apache网络服务器使一切正常工作在我的计算机上。 After uploading the files to an online website, the ajax function does not kick in. I seems like the e.preventDefault(); 将文件上传到在线网站后,ajax函数无法启动。我好像是e.preventDefault(); stops working after the upload, and the form is redirected to a new site,and not just being processed on the site without the reload. 上载后停止工作,并且表单被重定向到新站点,而不仅仅是在不重新加载的情况下在站点上进行处理。

I have also been trying to use the return false; 我也一直在尝试使用return false; instead of e.preventDefault(); 而不是e.preventDefault(); without any success. 没有任何成功。

Her is my code: 她是我的代码:

.html html的

<form method="post" action='mail/mail.php'>
    <label>Name</label>
    <input name="name" id="name" placeholder="Name.." required="true" class="input-field">

    <label>Mail</label>
    <input type="email" name="email" placeholder="Mail.." required="true" class="input-field">

    <label>Msg</label>
    <textarea name="message" id="message" class="textarea-field" required="true"></textarea>

    <input type="submit" id="submit" name="submit" value="Send">
</form>

    <div id="loading">
        Sender melding...
    </div>
    <div id="success">
    </div>

.js .js文件

$(function(){
      $('form').submit(function(e){
        var thisForm = $(this);
        //Prevent the default form action

        //return false;
        e.preventDefault();

        //Hide the form
        $(this).fadeOut(function(){
          //Display the "loading" message
          $("#loading").fadeIn(function(){
            //Post the form to the send script
            $.ajax({
              type: 'POST',
              url: thisForm.attr("action"),
              data: thisForm.serialize(),
              //Wait for a successful response
              success: function(data){
                //Hide the "loading" message
                $("#loading").fadeOut(function(){
                  //Display the "success" message
                  $("#success").text(data).fadeIn();
              });
            }
          });
        });
      });
    })

Please help! 请帮忙!

That's because your JS is missing a closing }); 那是因为您的JS缺少结尾}); . Please check this demo to confirm that the default action is indeed prevented and the ajax does kick in. However, I was expecting a POST but instead I am seeing an OPTIONS request. 请检查此演示以确认确实阻止了默认操作并且ajax确实启动了。但是,我期待着POST但是却看到了OPTIONS请求。

NOTE : Giving an element a name or id attribute value of submit is bad practice. 注意 :为元素提供nameid属性值submit是不好的做法。 You cannot for example use JavaScript to submit the form via default form submission -- this.submit() or $('form')[0].submit() without getting the error ...submit() is not a function .... . 例如,您不能使用JavaScript通过默认表单提交来提交表单this.submit() or $('form')[0].submit()不会出现错误...submit() is not a function ....

 $(function() { $('form').submit(function(e) { var thisForm = $(this); //Prevent the default form action //return false; e.preventDefault(); //Hide the form $(this).fadeOut(function() { //Display the "loading" message $("#loading").fadeIn(function() { //Post the form to the send script $.ajax({ type: 'POST', url: thisForm.attr("action"), data: thisForm.serialize(), //Wait for a successful response success: function(data) { //Hide the "loading" message $("#loading").fadeOut(function() { //Display the "success" message $("#success").text(data).fadeIn(); }); } }); }); }); }); }); // <==== MISSING THIS 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <form method="post" action='mail/mail.php'> <label>Name</label> <input name="name" id="name" placeholder="Name.." required="true" class="input-field"> <label>Mail</label> <input type="email" name="email" placeholder="Mail.." required="true" class="input-field"> <label>Msg</label> <textarea name="message" id="message" class="textarea-field" required="true"></textarea> <input type="submit" id="submit" name="submit" value="Send"> </form> <div id="loading"> Sender melding... </div> <div id="success"> </div> 

由于无论如何都是通过AJAX提交的,因此您可能会发现将输入类型更改为button并绑定为click而不是表单提交更容易,从而避免了您试图绕过的默认提交行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM