When using form to submit data: In HTML4, input, button and other form-related elements must be placed in the form element; in HTML5, this restriction no longer exists. Such elements can be linked to a form anywhere in the document (via the form attribute of the form element [Example 3 below]).
Example 1: New tab page displays form results
<!doctype html><html lang="en"><head> <meta charset="UTF-8"> <title>Form Target</title></head><body> <form action="http://localhost:8888/form/userInfo" enctype="application/x-www-form-urlencoded" target="_blank" method="post" autocomplete="off"> <p> <label for="username">用户名:</label> <input type="text" name="username" id="username" autofocus> </p> <p> <label for="password">密码:</label> <input type="password" id="password" autocomplete="on"><br> </p> <p> <label for="address">地址:</label> <input type="text" name="address" id="address" disabled value="北京市"> </p> <p> <input type="hidden" name="source" value="直接来源"> </p> <button>提交</button> </form></body></html>
action attribute of the form describes where the browser should send the data collected from the user when submitting [In the above example, the submitted data is sent to "http: //m.sbmmt.com/:8888/form/userInfo”]. If the action attribute specifies a relative URL, then the value will be grafted behind the URL of the current page (if the base element is used, the value of the href attribute of the element).
enctype attribute specifies the encoding method used by the browser for data sent to the server [In the above example, the default encoding method is used].
Description | |
---|---|
Default encoding method; except that it cannot be used to upload files to the server, it is suitable for various types of forms | |
Generally only used for forms that need to upload files to the server | |
Use with caution; each browser implements it differently |
attribute to automatically fill in the form; the default is on, and when set to off, the browser is prohibited from automatically filling in the form. The setting of the autocomplete attribute of each input element can override the behavior on the form element.
4. Specify the target display position of the form feedback information
Description | |
---|---|
will browse Display browser feedback information in a new window (or tab) | |
Display browser feedback information in the parent window group | |
Display browser feedback information in the current window (default behavior) | |
Display browser feedback information Information is displayed in the top-level window | |
Displays browser feedback information in the specified window frame |
值 | 说明 |
---|---|
submit | 提交表单(默认行为) |
reset | 重置表单 |
button | 无具体语义 |
表:type属性设置为submit时button元素的额外属性
属性 | 说明 |
---|---|
form | 指定按钮相关的表单 |
formaction | 覆盖form元素的action属性,另行指定表单将要提交到的URL |
formenctype | 覆盖form元素的enctype属性,另行指定表单的编码方式 |
formmethod | 覆盖form元素的method属性 |
formtarget | 覆盖form元素的target属性 |
formnovalidate | 覆盖form元素的novalidate属性,表明是否应执行客户端数据有效性检查 |
示例3:button元素提交表单
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>button属性控制表单</title></head><body> <form id="myForm"></form> <p> <label for="username">用户名:</label> <input type="text" name="username" id="username" form="myForm"> </p> <p> <label for="address">地址:</label> <input type="text" name="address" id="address" form="myForm"> </p> <button type="submit" form="myForm" formaction="http://localhost:8888/form/userInfo" formmethod="post">提交</button> <button type="reset" form="myForm">重置</button></body></html>
The above is the detailed content of HTML5-Summary of code examples for form usage. For more information, please follow other related articles on the PHP Chinese website!