JavaScript如何获得input元素value的值


在JavaScript中获取input元素value的值:

方法一:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta charset="utf-8">
 5     <meta http-equiv="X-UA-Compatible" content="IE=edge">
 6     <title>JavaScript中获取input元素value值</title>
 7     <link rel="stylesheet" href="">
 8     <script>
 9         function printInputValue() {
10             var inputValue = document.getElementById("demo1").value;
11             console.log(inputValue);
12             document.getElementById("demo2").innerHTML = inputValue;
13         }
14     </script>
15 </head>
16 <body>
17     <input type="text" id="demo1">
18     <button onclick="printInputValue()">获取input元素value值</button>
19     <p id="demo2"></p>
20 </body>
21 </html>
View Code

方法二:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta charset="utf-8">
 5     <meta http-equiv="X-UA-Compatible" content="IE=edge">
 6     <title>JavaScript34</title>
 7     <link rel="stylesheet" href="">
 8     <script>
 9         function print(){
10             var a = myform.name.value;
11             alert(a);
12             document.getElementsByName("span")[0].innerHTML = a;
13         }
14     </script>
15 </head>
16 <body>
17     <form name="myform">
18         <input type="text" name="name" id="nn">
19         <input type="button" name="button" value="测试" onclick="print()">
20         <span name="span"></span>
21     </form>
22 </body>
23 </html>
View Code

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM