Home Web Front-end HTML Tutorial Deep understanding of HTML forms

Deep understanding of HTML forms

Jan 23, 2017 pm 03:54 PM
html form

Form elements

From HTML to HTML5, form-related elements have been greatly expanded and can basically meet our common needs. . But in actual work, Due to interaction or browser compatibility requirements, native form elements sometimes have to be extended or simulated. But before that, it is still important to clearly understand and master the various form elements. in the text, We will explain form elements (HTML5 form elements by default) in detail.

form

##● The form will automatically handle the submit event (the submit event is usually triggered by the input or button element of type=submit)

●The form will automatically do a layer of verification, and use form.novalidate to turn off the native validate

●Form will obtain the corresponding user input based on the name of each form element, and then add the form data in the form of query string to the end of the URL corresponding to the action attribute. The default request method is GET, and the default action is the current url.

●event.target.elements will return all interactive elements

<form novalidate>
  <input name=&#39;username&#39; required/>
  <input name=&#39;passworkd&#39; type=&#39;password&#39; required/>
  <input name=&#39;email&#39; type=&#39;email&#39;/>
  <input type=&#39;submit&#39; value=&#39;submit&#39;/>
</form>
Copy after login

You can see by running the above code that after submitting the form, the browser address will Add a query string like this?username=tom&passworkd=123&email=test%40gmail.com

Interactive elements

Need to interact with the user and obtain user input We classify this type of form elements as interactive form elements.

Some are listed below:

●input: Commonly used types include checkbox, tel, number, email, etc.

●textarea

●select

●option

Feedback elements

Form elements that simply feed back information and do not require interaction with the user, we classify them as Feedback form element.

Some are listed below:

●meter

●output

<form oninput="result.value=parseInt(a.value)+parseInt(b.value)">
    <input type=&#39;number&#39; value=&#39;50&#39; name=&#39;a&#39; />
    <input type=&#39;number&#39; value=&#39;10&#39; name=&#39;b&#39; />
    <output name=&#39;result&#39;>60</output>
</form>
Copy after login

For output, you can set the calculated value in form.oninput

Grouped elements

This type of form element is used to group multiple form elements. We classify them as grouped form elements.

Some are listed below:

●fieldset

●optgroup

<form>
  <select>
    <optgroup label=&#39;group1&#39;>
      <option>1</option>
      <option>2</option>
      <option>3</option>
    </optgroup>
    <optgroup label=&#39;group2&#39;>
      <option>4</option>
      <option>5</option>
      <option>6</option>
    </optgroup>
    <optgroup label=&#39;group3&#39;>
      <option>7</option>
      <option>8</option>
      <option>9</optioin>
    </optgroup>
  </select>
</form>
Copy after login

label

●Use for can be connected to the corresponding interactive element associated with the id

●Can be used to wrap interactive elements, including multiple, control the first one

●It is not recommended to nest labels

<form>
  <fieldset>
    <legend>Title</legend>
    <label for=&#39;radio&#39;>Click me</label>
    <input type=&#39;radio&#39; id=&#39;radio&#39;/>
  </fieldset>
</form>
Copy after login
<form>
  <fieldset>
    <legend>用户信息</legend>
    <label>
      男 <input type=&#39;radio&#39; name=&#39;gender&#39; id=&#39;male&#39; />
    </label>
    <label>
      女 <input type=&#39;radio&#39; name=&#39;gender&#39; id=&#39;female&#39;/>
    </label>
  </fieldset>
</form>
Copy after login

Use JavaScript to process forms

Abstraction of field

The most basic structure:


 field: {
    name: String,
    value: String || String[]
  }
Copy after login

● String[] of value is usually used, and is divided and synthesized into a String

●For names with complex structures, you can use keyPath

  const fromKeyValues = {
    &#39;user.name&#39;: &#39;Tom&#39;,
    &#39;user.phone[0].number&#39;: &#39;123456&#39;,
    &#39;user.phone[0].type&#39;: &#39;mobile&#39;
  };

  const fromValues = {
    user: {
      name: &#39;Tom&#39;,
      phone: [
        {
          number: &#39;123456&#39;,
          type: &#39;mobile&#39;
        }
      ]
    }
  };
Copy after login

If you are not very clear about the above code, please refer to qs

A complete implementation

●Prevent form from default submit event

●checkbox needs to be checked instead of value

● select-multiple needs to store multiple values

●Except for the above special interactive elements, the others take their values ​​from value by default

form.html

<form>
    <fieldset>
        <legend>Login</legend>
        <input name=&#39;username&#39; placeholder=&#39;username&#39; minlength=&#39;2&#39;/>
        <input name=&#39;password&#39; type=&#39;password&#39; placeholder=&#39;password&#39;/>
        <label>
            remember password            <input name=&#39;checkbox&#39; type=&#39;checkbox&#39;/>
        </label>
    </fieldset>
    <fieldset>
        <p class="gender">
            <legend>More Info</legend>
            <label>
                男                <input name=&#39;gender&#39; type=&#39;radio&#39; value=&#39;male&#39; />
            </label>
            <label>
                女                <input name=&#39;gender&#39; type=&#39;radio&#39; value=&#39;female&#39; />
            </label>
        </p>
        <select name=&#39;select&#39; multiple>
            <option>1</option>
            <optgroup label=&#39;2&#39;>
                <option>2.1</option>
                <option>2.2</option>
            </optgroup>
        </select>
    </fieldset>
    <button type=&#39;submit&#39;>Submit</button></form>
Copy after login

form.js

var $form = document.querySelector(&#39;form&#39;);

function getFormValues(form) {
  var values = {};
  var elements = form.elements; // elemtns is an array-like object

  for (var i = 0; i < elements.length; i++) {
    var input = elements[i];
    if (input.name) {
      switch (input.type.toLowerCase()) {
        case &#39;checkbox&#39;:
          if (input.checked) {
            values[input.name] = input.checked;
          }
          break;
        case &#39;select-multiple&#39;:
          values[input.name] = values[input.name] || [];
          for (var j = 0; j < input.length; j++) {
            if (input[j].selected) {
              values[input.name].push(input[j].value);
            }
          }
          break;
        default:
          values[input.name] = input.value;
          break;
      }
    }

  }

  return values;
}

$form.addEventListener(&#39;submit&#39;, function(event) {
  event.preventDefault();
  getFormValues(event.target);
  console.log(event.target.elements);
  console.log(getFormValues(event.target));
});
Copy after login

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

See all articles