2014-12-07 155 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

相關問題