zoukankan      html  css  js  c++  java
  • JQuery下拉控件select的操作汇总

    JQuery获取和设置Select选项方法汇总如下:

    获取select

    先看看下面代码:

    1 $("#select_id").change(function(){//code...});   //为Select添加事件,当选择其中一项时触发
    2 var checkText=$("#select_id").find("option:selected").text();  //获取Select选择的Text
    3 var checkValue=$("#select_id").val();  //获取Select选择的Value
    4 var checkIndex=$("#select_id ").get(0).selectedIndex;  //获取Select选择的索引值
    5 var maxIndex=$("#select_id option:last").attr("index");  //获取Select最大的索引值
    6 $("#select_id ").get(0).selectedIndex=1;  //设置Select索引值为1的项选中
    7 $("#select_id ").val(4);   //设置Select的Value值为4的项选中
    8 $("#select_id option[text='jQuery']").attr("selected"true);   //设置Select的Text值为jQuery的项选中

    获取select 选中的 text :

    1 $("#ddlRegType").find("option:selected").text();

    获取select选中的 value:

    1 $("#nowamagic").val();

    获取select选中的索引:

    1 $("#nowamagic").get(0).selectedIndex;

    设置select

    jQuery添加/删除Select的Option项:

    1 $("#select_id").append("<option value='Value'>Text</option>");  //为Select追加一个Option(下拉项)
    2 $("#select_id").prepend("<option value='0'>请选择</option>");  //为Select插入一个Option(第一个位置)
    3 $("#select_id option:last").remove();  //删除Select中索引值最大Option(最后一个)
    4 $("#select_id option[index='0']").remove();  //删除Select中索引值为0的Option(第一个)
    5 $("#select_id option[value='3']").remove();  //删除Select中Value='3'的Option
    6 $("#select_id option[text='4']").remove();  //删除Select中Text='4'的Option

    设置select 选中的索引:

    1 //index为索引值
    2 $("#nowamagic").get(0).selectedIndex=index;

    设置select 选中的value:

    1 $("#nowamagic").attr("value","Normal");
    2 $("#nowamagic").val("Normal");
    3 $("#nowamagic").get(0).value = value;

    设置select 选中的text:

    1 var count=$("#nowamagicoption").length;
    2   for(var i=0;i<count;i++) 
    3      {           if($("#nowamagic").get(0).options[i].text == text) 
    4         
    5             $("#nowamagic").get(0).options[i].selected = true
    6            
    7             break
    8         
    9     }

    清空 select:

    1 $("#nowamagic").empty();
  • 相关阅读:
    用js写留言信息的判断非空条件
    tp3.2中怎么访问分类及子分类下面的文章
    关于PHP中的 serialize () 和 unserialize () 的使用(即关于PHP中的值与已存储的表示的相互转换)
    iOS 8 Auto Layout界面自动布局系列2-使用Xcode的Interface Builder添加布局约束
    iOS深入学习(Block全面分析)
    面试4
    Android
    android适配不同分辨率的手机
    android分辨率适配
    Android屏幕适配全攻略(最权威的官方适配指导)
  • 原文地址:https://www.cnblogs.com/xiaoyang002/p/4067807.html
Copyright © 2011-2022 走看看