如何集成表单插件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",
}
});
}
});