淺析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實現輸入框郵箱內容自動補全與上下翻動顯示效果【附demo源碼下載】
這篇文章主要介紹了jQuery實現輸入框郵箱內容自動補全與上下翻動顯示效果,涉及jquery.autoComplete.js插件的使用,可實現輸入框內容自動補全的功能,附帶了完整的demo源碼供讀者下載參考,需要的朋友可以參考下2016-09-09淺談jQuery的bind和unbind事件(綁定和解綁事件)
下面小編就為大家?guī)硪黄獪\談jQuery的bind和unbind事件(綁定和解綁事件)。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03