zoukankan      html  css  js  c++  java
  • 关于select 下拉框取值

    1.获取选中的下拉框的索引:

    var index = document.getElementById("ID").selectedIndex;//得到选中项的索引
    var department =document.getElementById("ID").options[index].text.trim();//得到选中项的text值
    var department1 =document.getElementById("ID").options[index].value.trim();//得到选中项的Value值

    2.获取相应的option的索引:

    var maxIndex = $(“#select_id :last”).get(0).index; //获取select最大索引值
    
    var checkIndex = $(“#select_id”).get(0).selectedIndex;//获取Select选中项的索引值
    
    var checkIndexs = $(‘option:selected’, ‘#select_id’).index(); //获取选中的select的索引
    
    var checkIndexa =('#select_id option').index(('#select_id option').index((‘#select_id option:selected’)); //获取选中的select的索引
    
    var checkIndex = $(‘#select_id’).prop(‘selectedIndex’); //获取选中的select的索引
    

    3.判断是否被选中:

    alert($(“#select_id”).find(“option[value=’Value值’]”).is(“:selected”)); //选中为true 没选中为false
    
    alert(document.getElementById(“select_id”).options[1].selected); //判断选中为true 没选中为false
    

    4.获取相应option的值:

    var checkValue = $(‘#select_id option:first’).val();//获取第一个option的值
    
    var checkValue =$(“#select_id option:last”).val();//最后一个option的值
    
    var checkValue = $(‘#select_id option:eq(1)’).val();//获取第二个option的值 

    5.禁用下拉框:

    $("#ID").attr("disabled","disabled");//禁用下拉框

     本人一般用得比较多的就是第一种和最后一种。

  • 相关阅读:
    Qt 学习之路 2(39):遍历容器
    Qt 学习之路 2(38):存储容器
    JS 格式化日期
    springboot 核心注解
    Java 生成随机数 Random、SecurityRandom、ThreadLocalRandom、Math.random()
    验证码 easy_captcha
    读过的书籍
    typora 常用快捷键
    kafka 遇到的问题
    老男孩Linux 运维
  • 原文地址:https://www.cnblogs.com/A-R-E-S/p/10482020.html
Copyright © 2011-2022 走看看