大家好,欢迎来到IT知识分享网。
的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(‘
Well done! You successfully read this important alert message.
‘);
});
// 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);
});
}
});
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/26226.html