2014-12-07 147 views
-1

我在同一页上有两个表单的页面。这两种形式基于视口显示(响应式设计/媒体查询CSS)。Ajax表单 - 在同一页面上从所有表单提交

在页面上,我使用bootstrapvalidator来验证字段,并通过Ajax为了在没有浏览器重新加载的情况下提交表单。因为bootstrapvalidator的,Ajax代码看起来是这样的 - 即针对所有形式的代码:

$('form').on('success.form.bv', function(e) { 
    var thisForm = $(this); 

    //Prevent the default form action 
    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(); 
      }); 
      } 
     }); 
     }); 
    }); 

这段代码的问题,因为它似乎是一个事实,即代码将发送两封邮件,一个用于可见的形式,一个隐藏的移动/选项卡形式 - 成功味精实际上将显示两种形式(当我调整浏览器的目标是台式机和暴民/平板电脑,我可以看到这两种形式的成功味精)。首先,我认为是因为e.preventDefault();有问题,然后我认为问题是由提交按钮的提交名称/编号name conflicts造成的。但是现在我很确定它与这两个表单在同一页面上的存在有关 - 因为我现在解决这个问题的唯一方法就是彻底删除其中的一个表单,这实际上不是解决方案!

我的形式看起来像这样以不同形式ID(html5form/html5formmob)和输入提交ID(暴民/台):

<form id="html5Form" method="post" action='mail/mail.php' 
      class="form-horizontal" 
      data-bv-message="This value is not valid" 
      data-bv-feedbackicons-valid="glyphicon glyphicon-ok" 
      data-bv-feedbackicons-invalid="glyphicon glyphicon-remove" 
      data-bv-feedbackicons-validating="glyphicon glyphicon-refresh">  

       <div class="form-group"> 
         <label>Name</label> 
         <input class="form-control" type="text" name="name" id="name" 
          data-bv-message="The username is not valid" 
          required data-bv-notempty-message="Please give a name"/> 
       </div> 

       <div class="form-group"> 
        <label>Mail</label> 
         <input class="form-control" name="email" id="email" type="email" required data-bv-emailaddress-message="No valid email" /> 
       </div> 

       <div class="form-group"> 
       <label>Msg</label> 
       <textarea class="form-control" name="message" id="message" rows="7" required 
       data-bv-notempty-message="No empty msg"></textarea> 
       </div> 

       <input type="submit" id="mob" value="Send"/> 
      </form> 
<div class="loading"> 
     Sending msg... 
    </div> 
    <div class="success"> 
    </div> 

所以我的问题,是有办法禁用/启用整个窗体使用CSS或JS? ..这可能是一个解决方案,或者你有其他建议吗?

回答

0

试图改变自己的JS部分成这样:

$('form').on('success.form.bv', function(e) { 
    var thisForm = $(this), parent = thisForm.parent(); 

    //Prevent the default form action 
    e.preventDefault(); 

    if (thisForm.is(':visible')) { 
     //Hide the form 
     thisForm.fadeOut(function() { 
     //Display the "loading" message 
     $(".loading", parent).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", parent).fadeOut(function() { 
         //Display the "success" message 
         $(".success", parent).text(data).fadeIn(); 
        }); 
       } 
      }); 
     }); 
    } 
}); 

将动态地工作,所以,即使你通过调整您的浏览器切换形式。

+0

谢谢您的回答!我尝试了你的建议..现在发生的事情与我删除'e.preventDefault();相同 - 默认表单操作正在发生,浏览器在提交时重新加载。 – Sepi 2014-12-08 07:54:30

+0

啊我看到了,我会在一分钟内更新我的答案 – redelschaap 2014-12-08 08:15:31

+0

我尝试了这个新代码,但不幸的是 - 仍然是同样的结果! – Sepi 2014-12-08 08:35:57

0

试试下面的办法:

$('form').on('success.form.bv', function(e) { 
var thisForm = $(this); 

//Prevent the default form action 
e.preventDefault(); 

if (getComputedStyle(thisForm[0], null).display != "none") 
{ 
    //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(); 
      }); 
      } 
     }); 
     }); 
    }); 
} 
}); 

但是你也可以通过自身验证每个表格:

$('#html5Form') 
    .formValidation({ 
     ... options ... 
    }) 
    .on('success.form.fv', function(e) { ... ajax call ...}); 

,甚至更好。将ajax调用包装成功能

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


    $('#html5Form') 
    .formValidation({ 
     ... options ... 
    }) 
    .on('success.form.fv', callAjax.bind(null, $(this))); 

    $('#html5formmob') 
    .formValidation({ 
     ... options ... 
    }) 
    .on('success.form.fv', callAjax.bind(null, $(this))); 
+0

谢谢你的回答!这也不起作用...什么关于基于屏幕大小使用JS删除/禁用表单操作? 'if(screen.width> 801){(“html5FormMob”)。removeAttr(“action”); } else {(“html5Form”)。removeAttr(“action”); }' – Sepi 2015-01-09 03:19:41

+0

@Sepi好想法,但你不想这样做。如果有人将您的网站加载到桌面上的小屏幕上,它会使用错误的表单。你实施了哪个解决方案? – Mouser 2015-01-09 10:25:53

+0

这是你的建议的结果: 解决方案1)Ajax工作 - 仍然是两封邮件。 解决方案2)Ajax无法正常工作 - 将页面重新加载到mail.php。 解决方案3)Ajax无法正常工作 - 将页面重新加载到mail.php。 在提交桌面表单之后,我意识到检查DOM的原因是,移动表单也显示加载和成功消息 - 可以通过某种方式触发mail.php两次启动吗? – Sepi 2015-01-09 12:20:08