Home Web Front-end H5 Tutorial Analysis of new attributes of forms in HTML5

Analysis of new attributes of forms in HTML5

May 21, 2017 pm 07:05 PM

This article mainly introduces the new attributes of HTML5 forms in detail, and attaches example analysis, which is simple and easy to understand. Friends who need it, let’s take a look together

1. The form attribute of the element in the form

In H5, you can put the form anywhere on the page, and then The element specifies a form attribute, and the attribute value is the id of the form, so that the element can be declared to belong to the specified form


<form id="testform">
     <input type="text">
    </form>
    <textarea form="testform"></textarea>
Copy after login

2. The elements within the form formaction attribute

In H4, the form submission method


<form action="1.jsp">
</form>
Copy after login

Submit the entire form

In H5, it can be Each attribute of the form is added to the jsp page submitted to


<form action="1.jsp">
     <input type="submit" action="1-1.jsp">提交到1页面</form>
     <input type="submit" action="1-2.jsp">提交到2页面</form>
     <input type="submit" action="1-3.jsp">提交到3页面</form>
    </form>
Copy after login

3. The formmethod attribute of the element within the form

can modify the form Specify the submission method for each element


<form action="1.jsp">
     <input type="submit" action="1-1.jsp" formmethod="post">提交到1页面,post提交方法</form>
     <input type="submit" action="1-2.jsp" formmethod="get">提交到2页面,get提交方法</form>
    </form>
Copy after login

4. The formenctype attribute of the elements in the form

is the same as above, and affects each element , the code is omitted

5. The formtarget attribute of the form

is the same as above, and acts on each element, the code is omitted

6. autofocus attribute

Add the autofocus attribute to text boxes, selection boxes, button controls, etc. When the screen is opened, it will automatically gain focus.


<input type="text" action="1-3.jsp" autofocus>提交到3页面</form>
Copy after login

Only one control on a page can have the autofocus attribute and cannot be abused

7. required attribute

can be used in most input elements. If the input value is empty, submission is not allowed. The browser displays text information to prompt the user that the content must be entered.

8. labels attribute

You can add a labels attribute to all elements of the form. The attribute value is a NodeList object, which represents the label elements bound to the element. collection.


<script type="text/javascript">
        window.onload = function () {
         var text = document.getElementById(&#39;text&#39;);
            var btn = document.getElementById(&#39;btn&#39;);
            var form = document.getElementById(&#39;form&#39;);
            btn.onclick = function() {
                if (text.value.trim() =="") {
                 //alert(text.labels.length)
                 if(text.labels.length==1) {
                  var label = document.createElement("label");
                  label.setAttribute("for","text");
                  form.insertBefore(label,btn);
                  text.labels[1].innerHTML = "请输入姓名";
                  text.labels[1].setAttribute(
               "style","font-size:9px;color:red");
                 }
                }
                else if(text.labels.length>1)
                 form.removeChild(text.labels[1]);
                }
        }
 </script>
    <form id="form"> 
     <label id="label" for="text">姓名:</label>
     <input id="text">
     <input id="btn" type="button" value="验证">
    </form>
Copy after login

When the user does not enter any value and clicks Verify, a label element will be dynamically added next to the text box. The label element is "Please enter a name"; when the user When there is input in the text box, the label with the text "Please enter your name" will be deleted.

9. The control attribute of the label

You can place a form element inside the label (label) and access the form element through the control attribute of the label.


<script type="text/javascript">
        function setValue() {
         console.log(1);
         var label =document.getElementById("label");
         var textbox = label.control;
         textbox.value = "231456"
        }
         </script>
             <form>
     <label id="label">
      邮编:
      <input id="txt_zip" maxlength="6">
      <small>请输入6位数字</small>
     </label>
     <input type="button" value="设置默认值" onclick="setValue()">
    </form>
Copy after login

Open the page in the browser, click the "Set Default Value" button, and 231456.

10 will be displayed in the text box. The placeholder attribute of the text box

When the text box (text or textarea) is in the selected state, enter the prompt message (gray)


<input type="text" palceholder="hahah">
Copy after login

11. The list attribute of the text box

Combined with the new datalist element of h5, create a prompt box


text:<input type="text" name="greeting" list="greetings">
    <!-- 使用style="display:none";将datalist元素设置为不显示 -->
    <datalist id="greetings" style="display:none">
     <option value="你是人">你是人</option>
     <option value="你是猪">你是猪</option>
     <option value="你是狗">你是狗</option>
    </datalist>
Copy after login

When you are in the text box When you enter "you" in , three columns of prompts "You are a human", "You are a pig", and "You are a dog" will appear below. If there are no keywords, no prompt will appear

12. Text The autocomplete attribute of the box

autocomplete has three values: "on", "off", and "" (not specified). If not specified, the browser's default value is used. The usage method is as follows


<input type="text" name="greeting" list="greetings" autocomplete="on">
Copy after login

When you enter "I am a human" in the text box and click submit, return to the previous page and enter "I" in the text box , the prompt "I am a human" will appear.

13. The pattern attribute of the text box

is equivalent to using regular expressions directly in the html part to determine whether the value input meets the requirements.

Please enter the content:

This code requires entering a number of three Capital letters, if entered incorrectly, the prompt "Please be consistent with what was requested (Google)" will appear.

14. The selectionDirection attribute of the text box

For the text and textarea elements of input, you can use it when the user selects part of the text with the mouse in these two elements. This element is used to obtain the selection direction.


<script type="text/javascript">
        function alertSelectionDirection() {
         var control = document.forms[0][&#39;text&#39;];
         var Direction = control.selectionDirection;
         alert(Direction);
        }
 </script>
 <form>
     <input type="text" name="text">
     <input type="button" value="惦记我" onclick="alertSelectionDirection()" >
        </form>
Copy after login

15. The indeterminate attribute of the check box

For the check box, in the past, there were only selection and non-selection Status, in HTML5, you can use the indeterminate attribute on this element in js to indicate the "not explicitly selected" state of the check box.


  <script>
        var cb = document.getElementById("cb");
        //将indeterminate的属性设置为true
        cb.indeterminate = true;
        </script>
        <input type="checkbox" indeterminate id="cb">indeterminate属性测试
Copy after login

16. The height attribute and width attribute of the image submit button

  • height: Specify the image button The height of the picture in the picture;

  • width: Specifies the width of the picture in the picture button;


<input type="image" src="img/2.png" width="23" height="23">
Copy after login

17. The maxlength attribute and wrap attribute of the textarea element

  • maxlength: Use integer value to set, used to limit the number of text that can be entered in the textarea element.

  • wrap: The attribute value can be specified as soft or hard. When the attribute is hard, the form is submitted when the text exceeds the specified range of a row and automatically wraps without using the Enter key. will add a newline flag at the newline.

The above is the detailed content of Analysis of new attributes of forms in HTML5. For more information, please follow other related articles on the PHP Chinese website!

Statement of this 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run the h5 project How to run the h5 project Apr 06, 2025 pm 12:21 PM

Running the H5 project requires the following steps: installing necessary tools such as web server, Node.js, development tools, etc. Build a development environment, create project folders, initialize projects, and write code. Start the development server and run the command using the command line. Preview the project in your browser and enter the development server URL. Publish projects, optimize code, deploy projects, and set up web server configuration.

What exactly does H5 page production mean? What exactly does H5 page production mean? Apr 06, 2025 am 07:18 AM

H5 page production refers to the creation of cross-platform compatible web pages using technologies such as HTML5, CSS3 and JavaScript. Its core lies in the browser's parsing code, rendering structure, style and interactive functions. Common technologies include animation effects, responsive design, and data interaction. To avoid errors, developers should be debugged; performance optimization and best practices include image format optimization, request reduction and code specifications, etc. to improve loading speed and code quality.

How to make h5 click icon How to make h5 click icon Apr 06, 2025 pm 12:15 PM

The steps to create an H5 click icon include: preparing a square source image in the image editing software. Add interactivity in the H5 editor and set the click event. Create a hotspot that covers the entire icon. Set the action of click events, such as jumping to the page or triggering animation. Export H5 documents as HTML, CSS, and JavaScript files. Deploy the exported files to a website or other platform.

What application scenarios are suitable for H5 page production What application scenarios are suitable for H5 page production Apr 05, 2025 pm 11:36 PM

H5 (HTML5) is suitable for lightweight applications, such as marketing campaign pages, product display pages and corporate promotion micro-websites. Its advantages lie in cross-platformity and rich interactivity, but its limitations lie in complex interactions and animations, local resource access and offline capabilities.

What is the H5 programming language? What is the H5 programming language? Apr 03, 2025 am 12:16 AM

H5 is not a standalone programming language, but a collection of HTML5, CSS3 and JavaScript for building modern web applications. 1. HTML5 defines the web page structure and content, and provides new tags and APIs. 2. CSS3 controls style and layout, and introduces new features such as animation. 3. JavaScript implements dynamic interaction and enhances functions through DOM operations and asynchronous requests.

How do I handle user location privacy and permissions with the Geolocation API? How do I handle user location privacy and permissions with the Geolocation API? Mar 18, 2025 pm 02:16 PM

The article discusses managing user location privacy and permissions using the Geolocation API, emphasizing best practices for requesting permissions, ensuring data security, and complying with privacy laws.

The difference between H5 and mini-programs and APPs The difference between H5 and mini-programs and APPs Apr 06, 2025 am 10:42 AM

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

Is H5 page production a front-end development? Is H5 page production a front-end development? Apr 05, 2025 pm 11:42 PM

Yes, H5 page production is an important implementation method for front-end development, involving core technologies such as HTML, CSS and JavaScript. Developers build dynamic and powerful H5 pages by cleverly combining these technologies, such as using the &lt;canvas&gt; tag to draw graphics or using JavaScript to control interaction behavior.

See all articles