我对评论系统进行了 AJAX 化,因此当Post Comment
单击按钮时,会进行 ajax 调用而不是原始表单提交。感谢我的另一个问题.submit 如果绑定对象由 ajax 刷新,则即使刷新提交按钮,它现在也可以工作。
现在我想在提交之前验证表单,但我无法让它工作。即使表单有效,也不会调用 submitHandler 选项。被alert( "Valid: " + commentform.valid() );
触发并且在执行原始提交之后不是ajax。
提交表单的 javascript 看起来像
jQuery(document).ready(function($){
$('#content_container').on("submit","#commentform",function(){
var commentform=$('#commentform'); // find the comment form
console.log('Post Comment button was clicked');
var validator = $("#commentform").validate({
onsubmit: false,
rules: {
author: "required",
email: { required: true, email: true },
url: { url: true },
comment: "required"
},
messages: {
author: "Please enter your name",
email: { required: "Please enter an email address",
email: "Please enter a valid email address" },
url: "Please enter a valid URL e.g. http://www.mysite.com",
comment: "Please include your comment"
} ,
submitHandler: function(form) { // The jQuery.validate plugin will invoke the submit handler if the validation has passed.
alert( "Valid (submitHandler): " + form.valid() );
console.log("before exit");
return;
console.log("after exit");
},
invalidHandler: function(event, validator) {
alert( "Valid (invalidHandler): " + form.valid() );
console.log("before exit");
return;
console.log("after exit");
}
});
alert( "Valid: " + commentform.valid() );
return;
$.ajax({
type: 'post',
url: formurl,
data: formdata,
有人可以建议如何在验证后进行 ajax 调用以提交表单吗?