js method to get form

  • 2020-06-07 04:01:24
  • OfStack

This article illustrates how js gets form. Share to everybody for everybody reference. The details are as follows:

Let's start with the following code:


<html> 
<head> 
<scirpy> 
window.onload=function(){ 
   var f1=document.f1; 
   var f2=document.forms[1]; 
 alert(f2.id); 
  var f3=document.forms['f1']; 
} 
</script> 
</head> 
<body> 
<form id="f1" value="f1"></from> 
<from id="f2" value="f2"></form> 
</body> 
</html>

Action form:


<html> 
 <head> 
 <script> 
  function checkform(f){ 
  var uname=f.username; 
  var pwd=f.password; 
  if(uname.value.length<4){ 
  alert(' User length must be greater than 4') ;  
  return false; 
  } 
  if(pwd.value.length!=6){ 
  alert(' User password must be greater than  6 position '); 
  return false; 
  } 
  return true; 
  } 
 </script> 
 </head> 
 <body> 
 <form id="f1" name="f1" method="post" action=""
 onsubmit="return checkform(this)"> 
   <input name="username" value="" /></br> 
  <input name="password" value="" /></br> 
  <input type="button" value=" submit " />  
 </form> 
 </body> 
</html>

js Three ways to operate form:

1. Use the form's index in the document or the form's name attribute to reference the form

document.forms[i]  // Gets the control in the page i A form 
document.forms[fromName] // Get the corresponding on the page name The form of

2. Use the id attribute of the form

document.getElementById(formId);

3.

document.formName;// Most commonly used 1 Kind of way 

Hopefully, this article has been helpful in your javascript programming.


Related articles: