2015-10-04 135 views
0

我的形式是自舉模式中定義和看起來像後未發送:POST jQuery的表單驗證

<form class="contact" name="contact" id="contact-form"> 
    <label class="modalLabel" for="name">Name</label><br> 
    <input type="text" name="name" class="input-xlarge" minlength="2" required><br> 
    <label class="modalLabell" for="email">E-mail Adresse</label><br> 
    <input type="email" name="email" class="input-xlarge" required><br> 
    <label class="modalLabel" for="message">Any message?</label><br> 
    <textarea name="message" class="input-xlarge"></textarea> 
    <div> 
    <input class="btn btn-success" type="submit" value="Absenden" id="submit"> 
    <a href="#" class="btn btn-warning" data-dismiss="modal">Doch nicht</a> 
    </div> 
</form> 

jQuery的對AJAX和驗證:

$("#contact-form").validate({ 
     submitHandler: function(form) { 
      $(form.contact).ajax({ 
         type: "POST", 
         url: "process.php", //process to mail 
         data: $('form.contact').serialize(), 
         success: function(msg){ 
          $("#thanks").html(msg) //hide button and show thank you 
          $("#form-content").modal('hide'); //hide popup 
         }, 
         error: function(){ 
          alert("Bitte versuchen Sie es nochmal."); 
         } 
      }); 
     } 
}); 

我得到了AJAX工作不通過將$.ajax呼叫放入click()函數進行驗證。在我用jQuery插件添加驗證後,POST不再工作。目前看起來GET已經完成了,我不知道如何才能正常工作。

+0

它是'$('form.contact')'或'$(form.contact)'? –

回答

1

$(form.contact).ajax({...很可能會拋出錯誤,因爲沒有選擇器特定的ajax方法,並且沒有對象form.contact。對於一類選擇,將需要大約form.contact

報價嘗試更改爲

$.ajax({... 
1

請查看工作樣例:http://codepen.io/anon/pen/ZbewGp

$("#contact-form").validate({ 
    submitHandler: function(form) { 
     $.ajax({ 
        type: "POST", 
        url: "process.php", //process to mail 
        data: $('form.contact').serialize(), 
        success: function(msg){ 
         $("#thanks").html(msg) //hide button and show thank you 
         $("#form-content").modal('hide'); //hide popup 
        }, 
        error: function(){ 
         alert("Bitte versuchen Sie es nochmal."); 
        } 
     }); 
    } 

});