Home > Web Front-end > JS Tutorial > body text

jQuery validate plug-in submitHandler submission causes infinite loop solution_jquery

WBOY
Release: 2016-05-16 15:19:05
Original
1497 people have browsed it

The example in this article describes the solution to the infinite loop caused by the jQuery validate plug-in submitHandler submission. Share it with everyone for your reference, the details are as follows:

There is no functional difference between the submission form.submit(); of the dom object and the submission $('').submit(); of the jquery object. But if you use the jquery validate plug-in, there will be a big difference between the two when submitting. When $('').submit(); is submitted, jquery validate will perform verification. If $('').submit(); is written in submitHandler, it will cause an infinite loop, but form.submit(); will not.

This problem is a small one. If you don’t pay attention or you are used to writing, it is easy to make mistakes; and when you make mistakes, it is difficult to find them. It took me nearly two hours to find the reason.

1. Let’s briefly talk about dom objects and jquery objects

dom object, you have to use js writing method to operate the tags in the page. As for jquery objects, you have to use jquery syntax to operate.

Converting a dom object into a jquery object is also very simple, just add $(), and then you can use jquery syntax to operate the tags on the page. I think this is what everyone uses the most. Take a look at an example.

$('input[name^="hour"]').each(function(index){
  this.value=index; //this是dom对像,js写法
});
$('input[name^="hour"]').each(function(index){
  $(this).val(index); //$(this)是jquery对像,jquery写法
});

Copy after login

2. jquery validate infinite loop program

$("#product_form").validate({
  focusInvalid:false,
  submitHandler: function(form){
    $('.submit').submit();
  }
});

Copy after login

Correct writing,

$("#product_form").validate({
  focusInvalid:false,
  submitHandler: function(form){
    form.submit();
  }
});

Copy after login

When an infinite loop occurs, the browser will continue to load until the browser hangs. At this time, you can add an alert, and you will know that validate is constantly verifying in a loop. Personally, I think that although it is a small problem, it is difficult to think of it.

Readers who are interested in more content related to jQuery plug-ins can check out the special topic of this site: "Summary of common jQuery plug-ins and usage"

I hope this article will be helpful to everyone in jQuery programming.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template