JavaScript form processing implementation code_basic knowledge
One form introduction
In HTML, the form is represented by the
document.getElementsByTagName('form')[0]; // Use to get the first element in the form element collection;
document.forms[0]; // Use the numeric subscript of forms to get the element;
document.forms['myForm']; // Use the form's name subscript to get the element;
2. Submit the form
(1). Through the event object, you can prevent the default behavior of submit. The default behavior of the submit event is to jump to the specified page with data;
addEvent(fm,'submit',function(evt){
preDef(evt);
});
(2). We can use the submit() method to customize the triggering of the submit event; that is, it is not necessary to click the submit button to submit;
If(e.ctrlKey && e.keyCode ==13){
}
// PS: Try to avoid using names such as name="submit" or id="submit" in the form. This will conflict with the submit() method and result in failure to submit;
// When a piece of data is submitted to the server, there will be a delay and no reflection for a long time, causing the user to keep clicking submit,
// As a result, many same requests are submitted repeatedly, or errors are caused or multiple pieces of the same information are written to the database;
preDef(evt);
Settimeout (function () {// 3 seconds before processing submitted to the server;
fm.submit();
},3000);
});
// Solve duplicate submission plan
// The first type: disable the click button immediately after submission;
Document.getElementById('sub').disabled = true; // Disable the button;
// Second type: After submission, cancel subsequent form submission operations;
var flag = false; var flag = false; //Set a listening variable;
if(flag == true)return; // If it exists, return the exit event;
flag = true; // Otherwise, it must be the first time, change the value of the listening variable;
3. Reset form
// 用户点击重置按钮时,表单会被初始化; // 虽然这个按钮还得以保留,但目前Web已经很少去使用了;因为用户填好信息后,不小心点击了重置就会全部清空,用户体验极差; // 有两种方法调用reset事件:第一个就是直接type="reset"即可;第二个就是使用fm.reset()方法调用; <input type="reset" value="重置" /> // 不需要JS代码即可实现; addEvent(document,'click',function(){ fm.reset(); // 使用JS方法实现重置; });
4.表单字段
// 表单处理中,建议使用HTMLDOM,它有自己的elements属性,该属性是表单中所有元素的集合;
fm.elements[0]; // 获取第一个表单字段元素;
fm.elements['user']; // 获取name值是user的表单字段元素;
fm.elements.length; // 获取所有表单字段的数量;
(1).共有的表单字段属性
// 除了

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

AI Hentai Generator
Generate AI Hentai for free.

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



PHP form processing: form reset and data clearing In web development, forms are a very important part and are used to collect data entered by users. After the user submits the form, we usually process the form data and perform some necessary operations. However, in actual development, we often encounter situations where we need to reset the form or clear the form data. This article will introduce how to use PHP to implement form reset and data clearing functions, and provide corresponding code examples. Form reset First, we need to understand the concept of form reset. when user

PHP form processing: form data sorting and ranking In web development, forms are a common user input method. After we collect form data from users, we usually need to process and analyze the data. This article will introduce how to use PHP to sort and rank form data to better display and analyze user-submitted data. 1. Form data sorting When we collect form data submitted by users, we may find that the order of the data does not necessarily meet our requirements. For those that need to be displayed or divided according to specific rules

PHP7 Form Processing Guide: How to use the $_REQUEST array to obtain form data Overview: When a user fills out a form on a web page and submits it, the server-side code needs to process the form data. In PHP7, developers can easily obtain form data using the $_REQUEST array. This article will introduce how to correctly use the $_REQUEST array to process form data, and provide some code examples to help readers better understand. 1. Understand the $_REQUEST array: $_REQUES

PHP form processing: form data export and printing In website development, forms are an indispensable part. When a form on the website is filled out and submitted by the user, the developer needs to process the form data. This article will introduce how to use PHP to process form data, and demonstrate how to export the data to an Excel file and print it out. 1. Form submission and basic processing First, you need to create an HTML form for users to fill in and submit data. Let's say we have a simple feedback form with name, email, and comments. HTM

How to use PHP to handle inline editing functions in forms Introduction: Forms are one of the commonly used elements in web development and are used to collect data entered by users. The inline editing function allows users to instantly edit and save data directly within the form, improving user experience and operational efficiency. This article will introduce how to use PHP to handle inline editing functions in forms, and attach corresponding code examples. 1. HTML part First, we need to create a form that contains inline editing functionality. In HTML, we can use content

How to use Vue form processing to implement disabling and enabling control of forms. In web development, forms are one of the indispensable components. Sometimes, we need to control the disabled and enabled status of a form based on specific conditions. Vue provides a concise and effective way to handle this situation. This article will introduce in detail how to use Vue to implement disabling and enabling control of the form. First, we need to create a basic Vue instance and a form. Here is a basic HTML and Vue code example: <divid=&

How to use Vue form processing to implement file upload of form fields Preface: In web applications, file upload is a very common requirement. Sometimes, we need users to upload files as part of the form fields, such as uploading user avatars, uploading pictures in comments, etc. It is very simple to use Vue to process and implement file uploads of form fields. In this article, we will introduce how to implement file upload using Vue form processing and provide code examples. Create a Vue component First, we need to create a Vue for file upload

How to implement multi-level drop-down box linkage in Vue form processing. When developing front-end applications, forms are one of the indispensable components. In forms, drop-down boxes, as a common selection input method, are often used to implement multi-level selection. This article will teach you how to achieve the linkage effect of multi-level drop-down boxes in Vue. In Vue, linkage of drop-down boxes can be easily achieved through data binding. Let's first look at a simple example. Suppose we have a city selector. The user needs to first select a province, and then based on the selection
