Home  >  Article  >  Web Front-end  >  HTML5-Summary of code examples for form usage

HTML5-Summary of code examples for form usage

黄舟
黄舟Original
2017-03-11 16:55:072029browse

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]).

1. Making a basic form

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>

HTML5-Summary of code examples for form usage

##1. The action attribute

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: //www.php.cn/: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).

2. Configure data encoding

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].

ValueDescription##application/x-www-form-urlencoded multipart/form-datatext/plain3. Control the form auto-complete function
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

autocomplete

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

By default, the browser will replace the original page where the form is located with the information fed back by the server after submitting the form. This can be changed using the target attribute of the form element [in the above example, the results will be displayed in a new tab].

Value_blank_parent_self_top

5. 设置表单名称

name属性可以用来为表单设置一个独一无二uerde标识符。
注意,input元素不设置name属性,那么用户在其中输入的数据在提交表单时不会被发送给服务器【上述示例中,“密码”字段不会被提交】。

6. 在表单中添加说明标签

可以为input元素配一个label元素,将label元素的for属性设置为input的id值,这样input元素和label元素就关联起来,有助于屏幕阅读器和其他残障辅助技术对表单的处理。

7. 自动聚焦到某个input元素

autofocus属性可以聚焦于某个input元素【上述示例中,“用户名”字段被自动聚焦】
注意,多个元素都设置了该属性,那么浏览器将会自动聚焦于其中的最后一个元素。

8. 禁用单个input元素

设置disabled属性,可以禁用input元素。
注意被禁用的元素不能被提交【上述示例中,“地址”字段被禁用未被提交到服务器】。

二、对表单元素编组

可以使用fieldset元素将一些元素组织在一起。
示例2:将相关表单元素进行编组

<!doctype html><html lang="en"><head>
    <meta charset="UTF-8">
    <title>元素分组</title></head><body>
    <form action="http://localhost:8888/form/userInfo" method="post" target="_blank">
       <fieldset>
           <legend>用户基本信息</legend>
           <p>
               <label for="username">用户名:</label>
               <input type="text" name="username" id="username">
           </p>
           <p>
               <label for="address">地址:</label>
               <input type="text" name="address" id="address">
           </p>
       </fieldset>
        <fieldset>
            <legend>用户爱好</legend>
            <p>
                <label for="fave1">#1:</label>
                <input type="text" name="fave1" id="fave1">
            </p>
            <p>
                <label for="fave2">#2:</label>
                <input type="text" name="fave2" id="fave2">
            </p>
            <p>
                <label for="fave1">#3:</label>
                <input type="text" name="fave3" id="fave3">
            </p>
        </fieldset>
        <button>提交</button>
    </form></body></html>

HTML5-Summary of code examples for form usage
说明

  • 通过设置fieldset元素的disabled属性,可以一次性地禁用多个input元素;

  • 添加lagend元素,可以向用户提供相关说明,但其必须为fieldset元素的第一个子元素。

三、使用button元素

表:button元素的type属性的值

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!

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