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.
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>
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>
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!