在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>
方法二:
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>