Home > Web Front-end > JS Tutorial > How to get form in js_javascript skills

How to get form in js_javascript skills

WBOY
Release: 2016-05-16 16:00:39
Original
2175 people have browsed it

The example in this article describes the method of obtaining form in js. Share it with everyone for your reference. The details are as follows:

Let’s look at the following code first:

<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>
Copy after login

Operation form:

<html> 
 <head> 
 <script> 
  function checkform(f){ 
  var uname=f.username; 
  var pwd=f.password; 
  if(uname.value.length<4){ 
  alert('用户长度必须大于4'); 
  return false; 
  } 
  if(pwd.value.length!=6){ 
  alert('用户密码必须大于 6位'); 
  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="提交" />  
 </form> 
 </body> 
</html>
Copy after login

Three ways to operate forms with js:

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

Copy code The code is as follows:
document.forms[i] //Get the i-th item in the page Form
document.forms[fromName] //Get the form of the corresponding name in the page

2. Use the id attribute of the form

Copy code The code is as follows:
document.getElementById(formId);

3.

Copy code The code is as follows:
document.formName;//The most commonly used method

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
js
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