JavaScript如何获得input元素value值

转载地址: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>
posted @ 2018-01-17 11:49  Fairyspace  阅读(73005)  评论(0编辑  收藏  举报