简体   繁体   English

Jquery联系表单多次发送

[英]Jquery contact form sends multiple times

I'm trying to create a jquery contact form that sends an ajax request when clicked. 我正在尝试创建一个jquery联系表单,在单击时发送ajax请求。

You can view it by visiting: http://childrensplaza.mn , and clicking the "click here to contact us button" 您可以访问: http//childrensplaza.mn查看它,然后单击“单击此处与我们联系按钮”

When testing this out though, After I click "send inquiry", it takes a while for the success message to show up, and I'm able to click it multiple times, causing my message to be sent multiple times. 在测试时,我点击“发送查询”后,成功消息显示需要一段时间,我可以多次点击它,导致我的消息被多次发送。

The code for it is below -> 其代码如下 - >

   <script>
    $(function(){
        $('#trigger').click(function(){
            $('#overlay').fadeIn(500);
            $('#form').fadeIn(500);
        });
        $('#overlay').click(function(){
            $('#form').fadeOut(500);
            $('#overlay').fadeOut(500);
        });
    });

    // Get the data from the form.  Check that everything is completed.
    $('#submit').click(function() {  
          var email = document.getElementById("email").value;
          var inquiry = document.getElementById("inquiry").value; 
          if( email == "" )
          {
            alert("Please enter your email.");
            return false;
          }
          if( inquiry == "" )
          {
            alert("Please enter your inquiry.");
            return false;
          }
        var dataString = 'email=' + email + '&inquiry=' + inquiry ; 
        //alert (dataString);return false;  
        $.ajax({  
          type: "POST",  
          url: "http://childrensplaza.mn/send.php",  
          data: dataString,  
          success: function() {  
           $('#success').fadeIn(500); 
          } 
        });  
        return false;  

    });
    </script> 

How would I make it so that the success message shows up on the first click, and I am not able to send the same request multiple times? 如何使第一次点击显示成功消息,并且我无法多次发送相同的请求?

This is easy enough to handle by adding a class when submitted the first time, and checking if the class is applied to determine whether or not to process the form. 这很容易通过在第一次提交时添加类来处理,并检查是否应用了类来确定是否处理表单。 If the button already has the class, do not continue to process. 如果按钮已经有类,请不要继续处理。

      if ( $(this).hasClass("pressed") ) return false;
      $(this).addClass("pressed");

Embedded in your code: 嵌入在您的代码中:

   <script>
    $(function(){
        $('#trigger').click(function(){
            $('#overlay').fadeIn(500);
            $('#form').fadeIn(500);
        });
        $('#overlay').click(function(){
            $('#form').fadeOut(500);
            $('#overlay').fadeOut(500);
        });
    });

    // Get the data from the form.  Check that everything is completed.
    $('#submit').click(function() {  
          var email = document.getElementById("email").value;
          var inquiry = document.getElementById("inquiry").value; 
          if( email == "" )
          {
            alert("Please enter your email.");
            return false;
          }
          if( inquiry == "" )
          {
            alert("Please enter your inquiry.");
            return false;
          }

          if ( $(this).hasClass("pressed") ) return false;
          $(this).addClass("pressed");

        var dataString = 'email=' + email + '&inquiry=' + inquiry ; 
        //alert (dataString);return false;  
        $.ajax({  
          type: "POST",  
          url: "http://childrensplaza.mn/send.php",  
          data: dataString,  
          success: function() {  
           $('#success').fadeIn(500); 
          } 
        });  
        return false;  

    });
    </script> 

You could take one step further by resetting the class after successful ajax response. 在成功执行ajax响应后,您可以通过重置类来更进一步。

           $('#success').fadeIn(500); $("#submit").removeClass("pressed");

you can create a flag and control it with the ajax events beforeSend and complete ... 你可以创建一个标志并用发送前的ajax事件来控制它并完成...

<script>
$(function(){
    $('#trigger').click(function(){
        $('#overlay').fadeIn(500);
        $('#form').fadeIn(500);
    });
    $('#overlay').click(function(){
        $('#form').fadeOut(500);
        $('#overlay').fadeOut(500);
    });
});
$('#submit').click(function() {  
var email = document.getElementById("email").value;
var inquiry = document.getElementById("inquiry").value; 
if( email == "" )
{
    alert("Please enter your email.");
    return false;
}
if( inquiry == "" )
{
    alert("Please enter your inquiry.");
    return false;
}
var dataString = 'email=' + email + '&inquiry=' + inquiry ;     
$.ajax({  
    type: "POST",  
    url: "http://childrensplaza.mn/send.php",  
    data: dataString,  

    beforeSend: function(xhr, opts){
        if($('#submit').hasClass("posting"))
            xhr.abort();
        else
            $('#submit').addClass("posting");
    },
    complete: function(){
        $('#submit').removeClass("posting");
    },
    success: function() {  
        $('#success').fadeIn(500); 
    } 
});  
return false;  

});
</script> 

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

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