如何集成表单插件http://malsup.com/jquery/form/和jquery验证插件http://docs.jquery.com/Plugins/Validation。我一直在尝试集成这两个插件,但我的问题是,如果你第一次单击按钮就会提交表单,即使字段为空,换句话说也就是如何忽略beforeSubmit。然后当第二次点击按钮时,beforeSubmit生效并启动验证。我无法看到我做错了什么。有人可以指出我正确的方向:
$(document).ready(function () {
$("#register_student_1 button").click(function (e) {
var options = {
target: '#reg-output1',
beforeSubmit: showRequest,
success: showResponse,
type: 'post'
};
$('#register_student_1').ajaxForm(options);
}),
function showResponse(responseText, statusText, xhr, $form) {
console.log("showResponse test");
}
function showRequest(formData, jqForm, options) {
$("#register_student_1").validate({
rules: {
first_name: "required",
last_name: "required",
date_of_birth: {
date: true,
required: true
},
address_1: "required",
nationality: "required",
city: "required",
country: "required",
email: {
required: true,
email: true
},
current_school_name: "required",
},
messages: {
first_name: "Please enter your first name",
last_name: "Please enter your last name",
date_of_birth: "Please enter your date of birth",
}
});
}
});