2015-04-23 54 views
0

在我的index.php页面上,表单动作属性为'target.php'。我使用jQuery Valdiate来验证表单。jQuery验证在指定div中提交

<form action="target.php" method="post" class="contact-validate"> 
    <div> 
     <div id="sent"></div> 
     <input type="text" placeholder="Przedstaw się" name="name" id="name" /> 
     <input type="text" placeholder="Miejscowość" name="city" id="city" /> 
     <button type="submit" id="submit">Send form *</button> 
    </div> 
</form> 

问题是,经过正确的验证,操作表单是作为其他页面执行的,但是我需要这个操作可以在#sent div中执行。

这里jQuery代码

<script> 
jQuery('.contact-validate').validate({ 
    rules: { 
     "name": { 
      required: true 
     }, 
     "city": { 
      required: true 
     } 
    }, 
    submitHandler: function(form) { 
     form.submit(); 
    } 
}); 
</script> 

我不知道我需要在“submitHandler”

+1

...要添加Ajax表单提交。 – Regent

+0

看到http://api.jquery.com/event.preventdefault/ –

+0

所以jquery验证没有自己的能力做到这一点?我需要使用ajax? – X9DESIGN

回答

0

,如果你想提交表单后剩余的页面无法使用form.submit()添加。相反,你可以使用这样的jQuery ajaxSubmit()功能,

HTML

<form action="target.php" id="theform" method="post" class="contact-validate"> 
    <div> 
     <div id="sent"></div> 
     <input type="text" placeholder="Przedstaw się" name="name" id="name" /> 
     <input type="text" placeholder="Miejscowość" name="city" id="city" /> 
     <button type="submit" id="submit">Send form *</button> 
    </div> 
</form> 

的Javascript

<script> 
jQuery('.contact-validate').validate({ 
    rules: { 
     "name": { 
      required: true 
     }, 
     "city": { 
      required: true 
     } 
    }, 
    submitHandler: function(form) { 
     $("#theform").ajaxSubmit({url: 'target.php', type: 'post'}); 
     $("#theform").submit(function(e){ // I think you need to prevent the actual (normal without ajax) form submitting. 
      e.preventDefault(); 
     }); 
    } 
}); 
</script> 
+0

'$ .validate'的'submitHandler'选项将阻止提交,如果您返回false,那么您可以这样做,而不是向窗体添加另一个'submit'事件处理程序。 – Ryley

0

为了防止表单提交,您可以使用

//(jQuery) prevent default functionality of the button 
e.preventDefault(); 

//(javascript) return from the method so it does not submit. 
return false; 

我不太确定后面那个。让我知道,一旦你尝试。

干杯!例如,

+0

第二个选项将起作用。 – Ryley