Deep understanding of HTML forms
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='username' required/> <input name='passworkd' type='password' required/> <input name='email' type='email'/> <input type='submit' value='submit'/> </form>
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●optionFeedback 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='number' value='50' name='a' /> <input type='number' value='10' name='b' /> <output name='result'>60</output> </form>
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='group1'> <option>1</option> <option>2</option> <option>3</option> </optgroup> <optgroup label='group2'> <option>4</option> <option>5</option> <option>6</option> </optgroup> <optgroup label='group3'> <option>7</option> <option>8</option> <option>9</optioin> </optgroup> </select> </form>
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='radio'>Click me</label> <input type='radio' id='radio'/> </fieldset> </form>
<form> <fieldset> <legend>用户信息</legend> <label> 男 <input type='radio' name='gender' id='male' /> </label> <label> 女 <input type='radio' name='gender' id='female'/> </label> </fieldset> </form>
Use JavaScript to process forms
Abstraction of fieldThe most basic structure:field: { name: String, value: String || String[] }
const fromKeyValues = { 'user.name': 'Tom', 'user.phone[0].number': '123456', 'user.phone[0].type': 'mobile' }; const fromValues = { user: { name: 'Tom', phone: [ { number: '123456', type: 'mobile' } ] } };
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 defaultform.html<form> <fieldset> <legend>Login</legend> <input name='username' placeholder='username' minlength='2'/> <input name='password' type='password' placeholder='password'/> <label> remember password <input name='checkbox' type='checkbox'/> </label> </fieldset> <fieldset> <p class="gender"> <legend>More Info</legend> <label> 男 <input name='gender' type='radio' value='male' /> </label> <label> 女 <input name='gender' type='radio' value='female' /> </label> </p> <select name='select' multiple> <option>1</option> <optgroup label='2'> <option>2.1</option> <option>2.2</option> </optgroup> </select> </fieldset> <button type='submit'>Submit</button></form>
form.js
var $form = document.querySelector('form'); 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 'checkbox': if (input.checked) { values[input.name] = input.checked; } break; case 'select-multiple': 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('submit', function(event) { event.preventDefault(); getFormValues(event.target); console.log(event.target.elements); console.log(getFormValues(event.target)); });

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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.

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

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

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

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

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

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