How to solve the infinite loop caused by the submitHandler of the jQuery validate plugin?
A possible reason for the infinite loop might be due to the code in submitHandler. You can try adding a flag in submitHandler to indicate if it has already been submitted, in order to prevent duplicate submissions.
var submitted = false;
$("#myForm").validate({
submitHandler: function(form) {
if (!submitted) {
submitted = true;
// 提交表单代码
form.submit();
}
}
});
Additionally, you can use “return false” in the submitHandler to prevent the default submission behavior and avoid duplicate submissions.
$("#myForm").validate({
submitHandler: function(form) {
// 提交表单代码
form.submit();
return false;
}
});
These are two possible solutions, choose the appropriate one based on the specific situation to avoid an infinite loop.