dongxie8906 2014-11-12 22:16
浏览 66
已采纳

使用e.preventDefault()联系表单; 不工作

UPDATE - the contact form is found at this 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. After uploading the files to an online website, the ajax function does not kick in. I seems like the 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; instead of e.preventDefault(); without any success.

Her is my code:

.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

$(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!

  • 写回答

2条回答 默认 最新

  • duanjiaoxi4928 2014-11-12 22:40
    关注

    That's because your JS is missing a closing });. 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.

    NOTE: Giving an element a name or id attribute value of submit is bad practice. 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 .....

    $(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>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件