Home  >  Article  >  Web Front-end  >  The submit event of the form Form does not respond_HTML/Xhtml_Web page production

The submit event of the form Form does not respond_HTML/Xhtml_Web page production

WBOY
WBOYOriginal
2016-05-16 16:45:491570browse

1. Problem description
When using JS to call the form method submit to submit the form directly, the submit event does not respond. Why? If you know, please reply. By analogy, I used input.select() to test, but it could respond to the select event. Putting this reason aside for now, let’s see how to solve the current problem first.
Code example that does not respond to events:



In actual operation, there will be no alert.
Although using the submit method to submit a form goes against the principles of Unobtrustive Javascript, it is sometimes necessary. For example, when making a search prompt (auto-complete) and selecting an Item, you need to use JS to submit the search form. 2. Problem Analysis

Since it does not respond to events, these events can only be triggered manually. Before determining the manual triggering plan, review the event registration methods:
There are two "original" registration methods, see the code example:




Such a registration event will add a method onsubmit to the form. Therefore, you can directly execute this method, which is equivalent to manually triggering the event. Look at the code example:


This can get an alert. But today’s “advanced” DOM2 standard registration method and IE’s registration method attachEvent are already very commonly used. For these registration methods, the onsubmit method does not exist. If you use form.onsubmit(), an error will be reported directly.

3. Solution Of course, the "advanced" registration method itself also provides a solution for manually triggering events. It just requires writing different programs for the DOM2 standard and IE. In addition, this program is also effective for the "original" registration method. Please see the code example:


4. Code Summary
Here we will not explain the details of each method. Friends who are not familiar with it should check the relevant information by themselves. Let's string the entire code together:




submit






There is a small problem in the whole process. Under FX, there is no need for form.submit() and the form is submitted directly, so this sentence is also omitted. If you know the reason, please reply. This demo was tested under IE6/IE7/FX and passed.
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