Home > Web Front-end > JS Tutorial > body text

JavaScript note form processing

高洛峰
Release: 2016-11-26 09:53:30
Original
904 people have browsed it

If you need to collect information from users of a website, you need to use a form.
Forms can contain most common graphical interface elements, including input fields, radio buttons, check boxes, pop-up menus, and input lists. In addition, HTML forms can contain password fields, which hide the user's input from prying eyes.
After filling in the form, click the Submit button on the menu to send the form information to the Web server. On the server, the CGI program (Common Gateway Interface, which is a script running on the Web server) will interpret and operate the data. The data is then often stored in a database for later use. Before using the data on the server side, you need to ensure that the data entered by the user is in an accurate (correct) format.
JavaScript is a great way to check data. This technique is called form validation. While verification can be done with CGI (and should be done as a precaution since some users turn off JavaScript in their browsers), verification with JavaScript on the client machine is much faster and more efficient for the user.
    Some summary about the form:
  Get the form
[javascript]
var form=document.forms["form1"];
var form=document.fors[0];

form object event
onsubmit is triggered before the form is submitted
onreset is triggered before the form is reset
Reference form elements
[javascript]
var element=tForm.elements[idex];
var element=tForm.elements[elementName];

Traverse all form fields in a form
[javascript]
for(var i=0;i //
}

Common properties of form fields
1) Create only Read (unavailable) form field
element.disabled=true;
element.disabled=false;
2) Get the value of the form field
value
Common method of form field
1) The form field gets focus: focus()
2 ) The form field loses focus: blur()

Common events for form fields
onFocus: This event is generated when the focus is obtained
onBlur: This event is generated when the focus is lost
onselect: This event is generated after the text is highlighted (selected) The file
onchange: When the value of the form field changes
onclick: Keyboard click
onkeydown: Keyboard pressed
onkeyup: Keyboard released
onkeypress: Keyboard pressed and released
onmouseover: Mouse moved up
onmouseout: Mouse moved out
onmousedown :Mouse down
onmouseup:Mouse up
Text fields, check boxes, radio buttons, drop-down list boxes, form validation, etc.单 Dynamically changing the menu 地 often needs to provide the user with the opportunity to enter the user through the pop -up menu, and hope to change one or more pop -up menu content according to the choice made by the user in another pop -up menu. Select the number of days in the selected month and fill in the second pop-up menu:
[html]


< title>Dynamic Menus







Select a value in the popup menu, and then you can create the content of the second popup menu
[javascript]
/**
* Dynamically change the menu
*/
window.onload=initForm;

function initForm(){
document. getElementById("months").selectedIndex=0;
document.getElementById("months").onchange=populateDay;
}

function populateDay(){
var monthDays=new Array(31,28,31,30,31 ,30,31,31,30,31,30,31);
var monthStr=this.options[this.selectedIndex].value;
//Use this (the month selected by the user in the first menu) from the menu Get the value in monthStr and store it in monthStr
if(monthStr!=""){
var theMonth=parseInt(monthStr);
document.getElementById("days").options.length=0;
for(var i=0;i                              document.getElementById("days").options[i]=new Option(i+1);

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!