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;来获取下拉列表的个数。
  • 相关阅读:
    CodeForces 1294B Collecting Packages(排序+贪心)
    计算系数(组合数)
    垒骰子(矩阵快速幂)
    蒜头君倒水(矩阵快速幂)
    Buy Tickets POJ
    Billboard HDU
    树状数组求逆序对 附HDU1394
    codeforces 1304D Shortest and Longest LIS
    codeforces 1301C Ayoub's function
    Codeforces 1301B Motarack's Birthday(二分)
  • 原文地址:https://www.cnblogs.com/ada313/p/1510013.html
Copyright © 2011-2022 走看看