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;

發佈了21 篇原創文章 · 獲贊 21 · 訪問量 64萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章