How to get the value of the input element in JavaScript


Reprint address: http://aquarius-zf.iteye.com/blog/605144

The most common page element in the page is the input, but how do we use JavaScript to get the value entered in the input of the web page? It is actually very simple, and there are more than one method. According to my summary, the most commonly used are the following two methods. , I won't say much more gossip, let's take a look at the method I said: 
Method 1, 

Java code   Favorite code
  1. <html>  
  2.  <head>  
  3.  <script language="javascript">  
  4.   function print(){  
  5.    var a=myform.name.value;  
  6.    alert(a);  
  7.   }  
  8.  </script>  
  9.  </head>  
  10.  <body>  
  11.   <form name="myform">  
  12.    <input type="text" name="name" id="nn" />  
  13.    <input type="button" name="button" value="获取" onclick="print()" />  
  14.   </form>    
  15.  </body>  
  16. </html>  
This is one of the ways to get the input value of a web page. Give from a name and then in JavaScript, you can use the name of the form to call the value attribute of the input element in the form to get the value, and assign the value to a, and finally use JavaScript The alert() print method prints out. 

Method Two, 

JS code   Favorite code
  1. <html>  
  2.  <head>  
  3.  <script language="javascript">  
  4.   function print(){  
  5.    var a=document.getElementById("nn").value;  
  6.    alert(a);  
  7.   }  
  8.  </script>  
  9.  </head>  
  10.  <body>  
  11.   <form>  
  12.    <input type="text" name="name" id="nn" />  
  13.    <input type="button" name="button" value="获取" onclick="print()" />  
  14.   </form>    
  15.  </body>  
  16. </html>  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325964658&siteId=291194637