Home > Java > javaTutorial > body text

How to get the value of input in js

(*-*)浩
Release: 2020-09-18 15:37:38
Original
40245 people have browsed it

JS method to get the input value: first name from; then use the name of the form in JavaScript to call the value attribute of the input element in the form form; then assign the value to "a"; finally use js The "alert()" method can print it out.

How to get the value of input in js

In fact, there are many ways to obtain input values ​​using js. You can do it by just using selectors, but the steps are still the same. Yes, I will recommend 2 types for you today.

Method 1:

<html>  
 <head>  
 <script language="javascript">  
  function print(){  
   var a=myform.name.value;  
   alert(a);  
  }  
 </script>  
 </head>  
 <body>  
  <form name="myform">  
   <input type="text" name="name" id="nn" />  
   <input type="button" name="button" value="获取" onclick="print()" />  
  </form>    
 </body>  
</html>
Copy after login

This is one of the ways to get the input value of the web page. Give from a name and then call it with the name of the form in JavaScript. The value attribute of the input element in the form can get the value, assign the value to a, and finally print it out using the JavaScript alert() printing method.

Method 2:

<html>  
 <head>  
 <script language="javascript">  
  function print(){  
   var a=document.getElementById("nn").value;  
   alert(a);  
  }  
 </script>  
 </head>  
 <body>  
  <form>  
   <input type="text" name="name" id="nn" />  
   <input type="button" name="button" value="获取" onclick="print()" />  
  </form>    
 </body>  
</html>
Copy after login

This is one of the ways to get the input value of the web page. Give from an ID and then use the form ID to call it in JavaScript. The value attribute of the input element in the form can get the value, assign the value to a, and finally print it out using the JavaScript alert() printing method.

The above is the detailed content of How to get the value of input in js. For more information, please follow other related articles on the PHP Chinese website!

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