JavaScript如何获得input元素value值

来源:互联网 发布:篮彩分析软件 编辑:程序博客网 时间:2024/04/29 15:15

转载地址:http://aquarius-zf.iteye.com/blog/605144

在页面中我们最常见的页面元素就是input了,但是我们如何用JavaScript得到网页input中输入的value值呢,其实很简单,方法也不止一种,据我总结比较常用的就是下面的两种方法,闲话不多说了,下面那就来看看我说的方法吧: 
方法一、 

Java代码  收藏代码
  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>  
这是获取网页input值的方法之一,给from一个名字然后在JavaScript的地方就可以用form的名字来调用form表单里input元素的value属性可以得到值,并把值赋给a,最后用JavaScript的alert()打印方法打印出来。 

方法二、 

JS代码  收藏代码
  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>  

0 2
原创粉丝点击