2015-10-04 94 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."); 
        } 
     }); 
    } 

});