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>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='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[] }● 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 = { 'user.name': 'Tom', 'user.phone[0].number': '123456', 'user.phone[0].type': 'mobile' }; const fromValues = { user: { name: 'Tom', phone: [ { number: '123456', type: 'mobile' } ] } };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 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)); });

HTML is not only the skeleton of web pages, but is more widely used in many fields: 1. In web page development, HTML defines the page structure and combines CSS and JavaScript to achieve rich interfaces. 2. In mobile application development, HTML5 supports offline storage and geolocation functions. 3. In emails and newsletters, HTML improves the format and multimedia effects of emails. 4. In game development, HTML5's Canvas API is used to create 2D and 3D games.

TheroottaginanHTMLdocumentis.Itservesasthetop-levelelementthatencapsulatesallothercontent,ensuringproperdocumentstructureandbrowserparsing.

The article explains that HTML tags are syntax markers used to define elements, while elements are complete units including tags and content. They work together to structure webpages.Character count: 159

The article discusses the roles of <head> and <body> tags in HTML, their impact on user experience, and SEO implications. Proper structuring enhances website functionality and search engine optimization.

The article discusses the differences between HTML tags , , , and , focusing on their semantic vs. presentational uses and their impact on SEO and accessibility.

Article discusses specifying character encoding in HTML, focusing on UTF-8. Main issue: ensuring correct display of text, preventing garbled characters, and enhancing SEO and accessibility.

The article discusses various HTML formatting tags used for structuring and styling web content, emphasizing their effects on text appearance and the importance of semantic tags for accessibility and SEO.

The article discusses the differences between HTML's 'id' and 'class' attributes, focusing on their uniqueness, purpose, CSS syntax, and specificity. It explains how their use impacts webpage styling and functionality, and provides best practices for


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

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
