Home  >  Article  >  Web Front-end  >  About the submit() function in jQuery

About the submit() function in jQuery

巴扎黑
巴扎黑Original
2017-06-29 10:28:231345browse

About submit() in jQueryFunction (Bind event handler or trigger event?)

I accidentally encountered one while typing code today It's a rather convoluted question (asked by a junior). I didn't understand it after thinking about it. I spent a long time on Baidu and Google in the morning, and read the English API before I finally understood it. Write it down now as a reminder to everyone. Without further ado, the code is as follows:


1  $('#myFormId').submit(function() { 
2      // submit the form 3      $(this).ajaxSubmit(); 
4      // return false,这样可以阻止正常的浏览器表单提交和页面转向 5      return false; 
6  });7  $('#myFormId').submit();

This section is similar to the code I encountered, when the program executes to the first line above The form submission event is not triggered, but the form is submitted when line 7 is executed. This led to the understanding of the following two functions:


1 $(selector).submit();2 $(selector).submit(fn);

After checking the API and searching on the Internet, I understood the whole story. The function in the first line above means to trigger the "submit" event of the selector-selected element, while the element in the second line means to bind the "submit" event processing function to the selector-selected element. In this way, we understand the meaning of the top code. The first line is to bind the processing function, and the seventh line of code is to trigger the event. OK, problem solved!

The following is the official API explanation in English. The official API explanation is reliable. I don’t understand the Chinese translation at all.

The three blue parts above are changes in different versions, and the ones circled in red explain their differences. English is relatively simple, so I won’t show off.

The above is the detailed content of About the submit() function in jQuery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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