• JQuery select控件的相关操作


    本文转载于 http://www.cnblogs.com/zfc2201/archive/2012/09/06/2674312.html

    JQuery获取和设置Select选项方法汇总如下:
    获取select
    先看看下面代码:
    $("#select_id").change(function(){//code...});   //为Select添加事件,当选择其中一项时触发
    var checkText=$("#select_id").find("option:selected").text();  //获取Select选择的Text
    var checkValue=$("#select_id").val();  //获取Select选择的Value
    var checkIndex=$("#select_id ").get(0).selectedIndex;  //获取Select选择的索引值
    var maxIndex=$("#select_id option:last").attr("index");  //获取Select最大的索引值
    $("#select_id ").get(0).selectedIndex=1;  //设置Select索引值为1的项选中
    $("#select_id ").val(4);   //设置Select的Value值为4的项选中
    $("#select_id option[text='jQuery']").attr("selected", true);   //设置Select的Text值为jQuery的项选中
    
    获取select 选中的 text :
    $("#ddlRegType").find("option:selected").text();
    
    获取select选中的 value:
    $("#nowamagic").val();
    
    获取select选中的索引:
    $("#nowamagic").get(0).selectedIndex;
    
    设置select
    jQuery添加/删除Select的Option项:
    $("#select_id").append("<option value='Value'>Text</option>");  //为Select追加一个Option(下拉项)
    $("#select_id").prepend("<option value='0'>请选择</option>");  //为Select插入一个Option(第一个位置)
    $("#select_id option:last").remove();  //删除Select中索引值最大Option(最后一个)
    $("#select_id option[index='0']").remove();  //删除Select中索引值为0的Option(第一个)
    $("#select_id option[value='3']").remove();  //删除Select中Value='3'的Option
    $("#select_id option[text='4']").remove();  //删除Select中Text='4'的Option
    设置select 选中的索引:
    
    //index为索引值
    $("#nowamagic").get(0).selectedIndex=index;
    
    设置select 选中的value:
    $("#nowamagic").attr("value","Normal");
    $("#nowamagic").val("Normal");
    $("#nowamagic").get(0).value = value;
    
    设置select 选中的text:
    var count=$("#nowamagicoption").length;
    
      for(var i=0;i<count;i++) 
      {           
        if($("#nowamagic").get(0).options[i].text == text) 
           { 
                $("#nowamagic").get(0).options[i].selected = true; 
                break; 
           } 
      }
    
    清空 select:
    $("#nowamagic").empty();
  • 相关阅读:
    Factorial Trailing Zeroes
    Convert Integer A to Integer B
    函数防抖、函数节流
    localstorage sessionstorage和cookie的区别
    element中表格中的表头与表格内容边框错位的解决方法
    解决Minio生成图片文件的分享链接无法正常下载的问题
    gin编写后端API的使用技巧
    YOLOV5源码解读-export.py网络结构、配置文件
    《三、YOLOV3细节原理全解析》
    《二、YOLOV2细节原理全解析》
  • 原文地址:https://www.cnblogs.com/laoji1109/p/3758334.html
Copyright © 2020-2023  润新知