的submitHandler的工作是提交表单,不登记表单提交事件处理程序。
当formm提交事件被触发时,submitHandler会被调用,而不是提交您正在注册提交处理程序的表单,因此当表单提交事件首次被触发时,表单未被提交。当第一次触发提交事件时,验证器会处理提交事件,然后触发您注册的处理程序,触发ajax请求。
在你只需要在submitHandler发送Ajax请求没有必要注册事件处理
$("#add-form").validate({
submitHandler: function (form) {
// setup some local variables
var $form = $(form);
// let's select and cache all the fields
var $inputs = $form.find("input, select, button, textarea");
// serialize the data in the form
var serializedData = $form.serialize();
// let's disable the inputs for the duration of the ajax request
$inputs.prop("disabled", true);
// fire off the request to /form.php
request = $.ajax({
url: "forms.php",
type: "post",
data: serializedData
});
// callback handler that will be called on success
request.done(function (response, textStatus, jqXHR) {
// log a message to the console
console.log("Hooray, it worked!");
alert("success awesome");
$('#add--response').html('<div class="alert alert-success"><button type="button" class="close" data-dismiss="alert">×</button><strong>Well done!</strong> You successfully read this important alert message.</div>');
});
// callback handler that will be called on failure
request.fail(function (jqXHR, textStatus, errorThrown) {
// log the error to the console
console.error(
"The following error occured: " + textStatus, errorThrown);
});
// callback handler that will be called regardless
// if the request failed or succeeded
request.always(function() {
// reenable the inputs
$inputs.prop("disabled", false);
});
}
});
什么是**其他页面的** js应该是?请显示足够的代码来对问题进行简要演示。您的OP不应该依赖外部链接来显示所有相关的代码。除非在“click”或“submit”处理程序中错误地封装了'.validate()',否则jQuery Validate插件只需要_one_ click。在你的情况下,你错误地在插件默认的'submitHandler'中包装了一个'submit'处理程序。由于插件的'submitHandler'回调已经捕获了提交事件,因此您附带的'submit'处理程序是多余的。 – Sparky