JavaScript form processing implementation code_basic knowledge
WBOY
Release: 2016-05-16 16:04:26
Original
1235 people have browsed it
One form introduction
In HTML, the form is represented by the
element;
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;
(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;
(3). Repeat submission
// 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;
AddEvent(fm,'submit',function(evt){ // Simulate server delay;
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;
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