在向电子邮件提交表单之后,我会收到2封电子邮件1

这个问题在这里已经有了答案:

  • event.preventDefault()与返回false 11个答案

  • 我猜#subBusinessOne是一个表单提交按钮。 您正在发送AJAX请求,然后再次将该表单作为正常的HTTP请求提交。

    您应该检查表单是否已提交,然后阻止默认操作并发送AJAX请求,而不是检测按钮单击事件。 你的JS代码看起来像这样:

    app.controller('threeCtrl',function($scope){
        $("#businessFormOne").submit(function(e) {
    
        e.preventDefault(); // this is to avoid the actual submit
    
        var url = "businessFormOne.php"; 
        $.ajax({
                   type: "POST",
                   url: url,
                   data: $("form#businessFormOne").serialize(), 
                   success: function(data)
                   {
                       var name = $("input[name=name]").val("");
                       var rel= $("input[name=phone]").val("");
                   }
                 });
            });
        });
    

    app.controller('threeCtrl',function($scope){
        $("#subBusinessOne").submit(function(e) {
        e.preventDefault();
        var url = "businessFormOne.php"; 
        $.ajax({
                   type: "POST",
                   url: url,
                   data: $("form#businessFormOne").serialize(), 
                   success: function(data)
                   {
                       var name = $("input[name=name]").val("");
                       var rel= $("input[name=phone]").val("");
                   }
                 });
            });
        });
    

    使用.preventDefault()

     app.controller('threeCtrl',function($scope){
            $("#subBusinessOne").click(function(e) {
            e.preventDefault();
            var url = "businessFormOne.php"; 
            $.ajax({
                       type: "POST",
                       url: url,
                       data: $("form#businessFormOne").serialize(), 
                       success: function(data)
                       {
                           var name = $("input[name=name]").val("");
                           var rel= $("input[name=phone]").val("");
                       }
                     });
            return false; // avoid to execute the actual submit of the form.
                });
            });
    
    链接地址: http://www.djcxy.com/p/19519.html

    上一篇: after submitting a form to email i get 2 email instead 1

    下一篇: what e.preventDefault() method really does?