Jquery中文网 www.jquerycn.cn
Jquery中文网 >  jQuery  >  jquery 教程  >  正文 浅析JQuery获取和设置Select选项的常用方法总结

浅析JQuery获取和设置Select选项的常用方法总结

发布时间:2014-02-06   编辑:www.jquerycn.cn
本篇文章是对JQuery获取和设置Select选项的一些常用方法进行了汇总介绍,有需要的朋友可以参考一下

1.获取select 选中的 text:
 $("#cusChildTypeId").find("option:selected").text();
 $("#cusChildTypeId option:selected").text()

2.获取select选中的 value:
 $("#ddlRegType ").val();

3.获取select选中的索引:
      $("#ddlRegType ").get(0).selectedIndex;

4.得到select项的个数  
 $("#cusChildTypeId").get(0).options.length

5.设置select 选中的索引:
     $("#cusChildTypeId").get(0).selectedIndex=index;//index为索引值

6.设置select 选中的value:
    $("#cusChildTypeId").attr("value","Normal");
    $("#cusChildTypeId").val("Normal");
    $("#cusChildTypeId").get(0).value = "Normal";

7.设置select 选中的text:
 1>.var count=$("#cusChildTypeId").get(0).options.length;
     for(var i=0;i<count;i++) 
         {          
  if($("#cusChildTypeId").get(0).options.text == text) 
         { 
             $("#cusChildTypeId").get(0).options.selected = true;
             break; 
         } 
        }

 2>.$("#cusChildTypeId").val(text);
    $("#cusChildTypeId").change();

8.向select中添加一项,显示内容为text,值为value  
 $("#cusChildTypeId").get(0).options.add(new Option(text,value));

9.删除select中值为value的项
        var count = $("#cusChildTypeId").size();          
        for(var i=0;i<count;i++)  
        {  
            if($("#cusChildTypeId").get(0).options[i].value == value)  
            {  
                $("#cusChildTypeId").get(0).remove(i);  
                break;  
            }
        }

10.清空 Select:
 1>. $("#cusChildTypeId").empty();
 2>. $("#cusChildTypeId").get(0).options.length = 0;  

您可能感兴趣的文章:
浅析JQuery获取和设置Select选项的常用方法总结
jQuery动态添加删除select项(实现代码)
浅析jQuery对select操作小结(遍历option,操作option)
jquery操作select详解(取值,设置选中)
jquery 获取表单元素里面的值示例代码
jquery操作select中option选项的方法汇总
JQuery下拉控件select操作方法大全
jquery获取元素值的方法(常见的表单元素)
jquery获取form表单元素值的实例
jquery操作select取值赋值与设置选中

关键词: jquery  select   
[关闭]