Summary of methods for form and form submission operations in HTML
小云云
Release: 2017-12-11 10:10:58
Original
3134 people have browsed it
This article mainly introduces the knowledge of form elements and form submission in HTML. Friends who need it can refer to it. I hope it can help everyone.
form element
The DOM interface of the form element is HTMLFormElement, which inherits from HTMLElement, so it is similar to other HTML The element has the same default properties, but it also has several unique properties and methods of its own:
Attribute Value
Description
##accept-charset
The character set that the server can handle, multiple character sets are separated by spaces
action
Accepts the URL of the request. This value can be overridden by the input in the form element or the formaction attribute of the button element
elements
Collection of all controls in the form (HTMLCollection)
enctype
The requested encoding type, this value can be used by the input in the form element or the formenctype of the button element Property Override
length
The number of controls in the form
method
The HTTP request to send Type, usually "get" or "post", this value can be overridden by the input in the form element or the formmethod attribute of the button element
name
The name of the form
reset()
Reset all form fields to default values
submit()
Submit the form
target
The name of the window used to send requests and receive responses. This value can be overridden by the formtarget attribute of the input or button element in the form element
autocomplete
Whether to automatically complete form elements
Input element
The input element is a very widely used form element. Depending on the value of the type attribute, it has the following common uses:
Text input< ;input type="text" name=""> Submit input Radio button input Check box input Number Input The input box can only enter numbers, and the maximum and minimum values can be set. Range input is similar to number, but it will display a slider instead of an input box. Color input will pop up a color picker. Date input A date picker will pop up. email input is displayed as a text input box, and a customized keyboard will pop up. tel input is similar to email input url input is similar to email input, and a customized keyboard will pop up. The textarea element can create a multi-line text area.
The attribute values of cols and row represent the characters of the width and height of the text area respectively. . The select element and the option element are used together to create a drop-down menu.
radio
How to group? Just set different name attributes
Example:
Play games Write code
Male 女, These are two sets of radio
placeholder
that provide hints that describe the expected value of the input field. This prompt will be displayed when the input field is empty, and will disappear when the field receives focus.
type=hidden
Define hidden input. Hidden fields are not visible to the user. Hidden fields usually store a default value, and their values can also be modified by JavaScript. For example, it is used for security purposes, transmitting name and value values invisible to users to the background, allowing the background to perform verification to prevent page forgery.
Submit button
Adding a submit button to the form allows users to submit the form.
The following three buttons can trigger the submit event of the form when clicked:
The default value of the type button element in the specification is submit , but the default value under IE678 is button, so from the perspective of compatibility, it is necessary to manually add the type="submit" attribute to the button element.
submit event
Beginners may think that form submission is triggered by the click event of the submit button. In fact, this is not the case. The click event of the button element and the submit event of the form are in different The execution order in browsers is different, so in order to accurately control the form submission event, we will choose to perform verification and other operations in the submit event of the form.
form.addEventListener('submit', function (e) {
if (valid()) {
...
}
e.preventDefault()
})
Copy after login
When there is no one of the above three buttons in the form element, the user will not be able to submit the form (the Enter key is also invalid). At this time You can use the unique submit() method of the form element to submit the form. It should be noted that calling the submit() method will not trigger the submit event of the form element. Verification of the form and other operations should be done after calling before submit() method.
if (valid()) {
form.submit()
}
Copy after login
Form submission and user experience
Based on the popular ajax + cross-domain POST (CORS) technology, we It is possible to submit data directly to the server without using the form element. While this works, it degrades the experience in most cases.
JavaScript Form Validation
JavaScript can be used to validate input data in HTML forms before the data is sent to the server.
These typical form data validated by JavaScript are:
The above is the detailed content of Summary of methods for form and form submission operations in HTML. For more information, please follow other related articles on the PHP Chinese 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