zoukankan      html  css  js  c++  java
  • 获取text、select、radio、checkbox的值

    获得文本框的值:

    <input type = "text" name = "inputValue" id = "text1" style ="200px;"/>

    可以通过 document.getElementsByName("inputValue")[0].value = "this is a text for textValue!" 对文本框进行复制
    或者     document.getElementById("text1").value = "this is a text for textValue!" 

    获得下拉列表的值: 

    <select name = "selectValue" id = "select1" onchange = "DisValue()">
        
    <option value = "1">test1</option>
        
    <option value = "2">text2</option>
        
    <option value = "3">text3</option>
    </select>
    <div id = "test"></div>
    可以通过:selectValue.options[selectValue.selectedIndex].text来获取当前选中列表的值
    注意:document.getElementById("select1").value 获取的值是当前选中列表的value的值。
    如当前选中的是test1,那么document.getElementById("select1").value 的值就是"1"
    还可以 通过另外一种方法获取选中列表的值:
    function disValue()
    {
      var sel =document.getElementById("select1");
      for(var i=0;i
    <sel.length;i++)   
      {   
        if(sel.options[i].selected)   
        {   
          document.getElementById("test").innerHTML
    = sel.options[i].text;   
        
    }   
    }

    另外,可以通过sel.getElementsByTagName("option").length;来获取下拉列表的个数。
  • 相关阅读:
    关于JAVA中HashMap集合的的三种超不好记的便利方案
    浅谈面向对象三大特性
    新鲜出炉springmvc
    看看我们以前搞过的几个对象
    在java中使用JDBC访问数据库
    关于多线程的小例子,快速上手!无需停留!!!
    关于java异常处理的面试题
    关于java异常处理
    关于java的log4j配置
    总结
  • 原文地址:https://www.cnblogs.com/ada313/p/1510013.html
Copyright © 2011-2022 走看看