zoukankan      html  css  js  c++  java
  • js获取select标签选中的值

    <select name="" id="testSelect" onclick="test2()">
    <option value="1">a</option>
    <option value="2">b</option>
    <option value="3">c</option>
    <option value="4">d</option>
    </select>
    <script src="js/jquery.min.js"></script>
    <script>


    function test(){
    var obj = document.getElementById("testSelect"); //定位id
    console.log(obj);
    var index = obj.selectedIndex; // 选中索引
    console.log(index);
    var text = obj.options[index].text; // 选中文本
    console.log(text);
    var value = obj.options[index].value; // 选中值
    console.log(value);
    }
    function test1(){
    console.log($('#testSelect option:selected').text());//选中的文本

    console.log($('#testSelect option:selected') .val());//选中的值

    console.log($("#testSelect ").get(0).selectedIndex);//索引
    }
    function test2(){
    console.log($("#testSelect").find("option:selected").text());//选中的文本
    console.log($("#testSelect").find("option:selected").val());
    console.log($("#testSelect ").get(0).selectedIndex);
    }
    </script>

  • 相关阅读:
    LaTeX插入数学公式
    清除浮动的4种方式
    水平居中与垂直居中
    如何实现两三栏布局
    BFC
    flex弹性盒子
    盒模型
    Git
    jQuery设置disabled属性与移除disabled属性
    TP---where多条件查询
  • 原文地址:https://www.cnblogs.com/studyh5/p/7744088.html
Copyright © 2011-2022 走看看